Home > Net >  Convert Python numpy array to specific Matlab format
Convert Python numpy array to specific Matlab format

Time:08-26

I am trying to convert python data to matlab format. It is saving a matlab file that has a data_array of 3x7x5 double.

But I need a file with 1x3 Cell (matlab cell) and in each cell 7x5 double matrix should be there. I tried to create list of dictionaries using these array and than save to matlab format. But that didn't helped too.

import numpy as np
import scipy.io
mat1 = np.random.randint(1,100,35).reshape(7,5)
mat2 = mat1*10
mat3 = mat1*0.1
list_mat = [mat1, mat2, mat3]
scipy.io.savemat("test_py1.mat", {'dict_array': list_mat})

Description of sample matlab file; sample_mat = scipy.io.loadma('sample_mat.mat') type(sample_mat) => dic var1 = sample_mat['key'] type(var1) => numpy.ndarray var1.shape = 1 x a var2 = var1[0,0] type(var2) => numpy.ndarray var2.shape = m x n

CodePudding user response:

I suspect that the following will work.

import numpy as np
import scipy.io
mat1 = np.random.randint(1,100,35).reshape(7,5)
mat2 = mat1*10
mat3 = mat1*0.1
list_mat = np.empty(3,dtype = object)
list_mat[0] = mat1
list_mat[1] = mat2
list_mat[2] = mat3
scipy.io.savemat("test_py1.mat", {'dict_array': list_mat})
  • Related