5

I have a question about tuples. I use a program which dumps large output 3-D arrays in the .mat format. They work fine in MATLAB. They are of the newer .mat format (HDF5 based). Say I generate a 10x10x10 3D matrix of complex numbers in MATLAB and save it as trial.mat

a = rand(10,10,10) + 1i*rand(10,10,10);
save('trial.mat')

Now I try to load the variable a in Python. Here is what I do.

import numpy as np
import h5py
f = h5py.File('trial.mat','r')
a = np.array(f['a'])
print a
print np.shape(a)

Here are the last few lines of the output I get.

   (0.7551184371302334, 0.15987275885464014)
   (0.5261111725119932, 0.7314707154838205)
   (0.8501109979307012, 0.05369411719045292)
   (0.8160309248196381, 0.7143895270837854)]]]
(10L, 10L, 10L)

The data is a 3D array of tuples. How do I cast this into a 3D numpy array of complex numbers? I need it for my further analysis. The actual arrays are quite large (1.5 GB and higher). I mean (0.81, 0.71) should be usable as 0.81 + 0.71j

I use MATLAB R2010a and Python 2.7.7, part of the Anaconda 2.01 (x86-64) distribution, on Windows 7.

Maybe this is related to the following question, but I am not really sure. How to read a v7.3 mat file via h5py?

Help will be greatly appreciated.

1 Answer 1

4

I'm using matlab R2010b on Windows Server 2008, and I seem to need '-v7.3' to save into an hdf5 file. Without the option, I get "version 0x0100" and with the option, I get "version 0x0200".

a = rand(10,10,10) + 1i*rand(10,10,10);
a(10,10,10) % in my case, result is 0.8667 + 0.3673i
save('trial.mat', '-v7.3')

On the Python side (in my case running under Linux): use view to make the tuples into complex values, and then repeat with real values to check that the ordering is correct:

import numpy as np
import h5py
f = h5py.File('trial.mat','r')
zz = f['a'].value.view(np.double).reshape((10,10,10,2))
zzj = zz[:,:,:,0] + 1j*zz[:,:,:,1]
zzk = f['a'].value.view(np.complex)
np.all(zzk == zzj)  # result is "True"
zzj.shape  # result is (10, 10, 10)
zzk[9,9,9].imag == f['a'][9,9,9][1]  # result is "True"
zzk[9,9,9]  # result is (0.8667498969993187+0.36728591513136899j) consistent with Matlab

We see that:

  • zzj and zzk have the complex values correctly ordered into real and imag
  • Corresponding entries into zzj and zzk match the values in Matlab

I haven't compared times with huge arrays, but I bet converting the hdf5 object directly to complex is fastest.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! Works like a charm. I had no idea there was a method to do this in numpy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.