问题
I have a mat file which contains 2 different cells containing matrices of different size. I need to convert that data into a numpy array using h5py for an experiment (I'm new in h5py. I thought it was as easy as it is explained here
Reading the file works well, putting the data in the numpy array also works well, but I need the value representation of each position inside each matrix inside each cell, taking into account that when I print for example np.array(x[0][1])
, I receive just the reference to the array(< HDF5 object reference>, dtype=object)
and I need the values inside it...
It looks very tempting to convert the cell into a 3d matrix in matlab, however I don't count with root permissions to do anything in Matlab other than reading data and doing calculations (no saving anything).
If someone has already worked on this, or someone has any idea about how to get this data, I'm ready to listen to your advices.
回答1:
Looking for matlab
and h5py
I find
read matlab v7.3 file into python list of numpy arrays via h5py (2014)
Reading a Matlab's cell array saved as a v7.3 .mat file with H5py (2015)
How to read a v7.3 mat file via h5py? (2013)
MATLAB structures and cells don't map directly onto h5
or numpy
classes. So there's a tendency to embed them in object
arrays. This issue arises when reading old .mat
files with scipy.io.loadmat
as well. To pull an element out of an dtype=object
array you have use further indexing, which for 0d arrays can be a little tricky.
Let me illustrate
In [603]: a = np.arange(4)
Make a 1d array with 1 item, and insert a
In [604]: b = np.array([None], dtype=object)
In [605]: b[0] = a
In [606]: b
Out[606]: array([array([0, 1, 2, 3])], dtype=object)
retrieve a
with indexing or item
:
In [607]: b[0]
Out[607]: array([0, 1, 2, 3])
In [608]: b.item()
Out[608]: array([0, 1, 2, 3])
but if it's a 0d numpy array:
In [618]: c=np.array(None)
In [619]: c
Out[619]: array(None, dtype=object)
In [620]: c[()]=a
In [621]: c
Out[621]: array(array([0, 1, 2, 3]), dtype=object)
In [622]: c.item()
Out[622]: array([0, 1, 2, 3])
In [623]: c[()]
Out[623]: array([0, 1, 2, 3])
In this case you have to index with an empty tuple, ()
.
回答2:
You have to call the element as follow:
np.array(x)[0, 1]
来源:https://stackoverflow.com/questions/37300974/opening-a-mat-file-using-h5py-and-convert-data-into-a-numpy-matrix