Read the .mat file in Python. But the data form has changed

% save .mat file in the matlab train_set_x=1:50*1*51*61*23; train_set_x=reshape(train_set_x,[50,1,51,61,23]); save(['pythonTest.mat'],'train_set_x','-v7.3'); 

The data obtained in the matrix have a size (50,1,51,61,23).

I am loading a .mat file in Python with this instruction.

The code is as follows:

 import numpy as np, h5py f = h5py.File('pythonTest.mat', 'r') train_set_x = f.get('train_set_x') train_set_x = np.array(train_set_x) 

The output train_set_x.shape file is (23L, 61L, 51L, 1L, 50L) . It is expected to be (50L, 1L, 51L, 61L, 23L) . So I changed the form to

 train_set_x=np.transpose(train_set_x, (4,3,2,1,0)) 

I'm interested in learning about data form changes between Python and Matlab. Are there any errors in my code?

+6
source share
1 answer

You have no errors in the code. There is a fundamental difference between Matlab and python in the way they handle multidimensional arrays.
Both Matalb and python store all the elements of a multi-disk array as a single continuous block in memory. The difference is in the order of the elements:
Matlab , (e.g. fortran) stores the elements in the first order, i.e. stores the elements according to the size of the array, for 2D:

  [1 3; 2 4] 

In contrast, Python stores items in the first order, starting with the last dimension of the array:

 [1 2; 3 4]; 

So, a block in memory with the size [m,n,k] in Matlab is considered by the python as an array of the form [k,n,m] .

See this wiki page for more information.

By the way, instead of transposing train_set_x you can try setting its order to "Fortran" (col-major, as in Matlab):

  train_set_x = np.array(train_set_x, order='F') 
+4
source

All Articles