我正在尝试在Python中创建一个Matlab单元格数组并将其保存为.mat文件,但是当所有单元格都包含2个值时,我遇到了问题:
这是什么原因呢?
import scipy.io as sio
twoValues = {'a': array([[array([[2, 2]]), array([[3, 3]])]])}
sio.savemat('test.mat',twoValues)
使用Matlab:
load('test.mat')
>>> a
a(:,:,1,1) =
2 3
a(:,:,1,2) =
2 3
>>> class(a)
ans =
int32
回到Python中:
threeValues = {'a': array([[array([[2, 2, 2]]), array([[3, 3]])]])}
sio.savemat('test.mat',threeValues)
在Matlab中:
>>> a
a =
[3x1 int32] [2x1 int32]
>>> class(a)
ans =
cell
这是什么原因呢?
array()
试图以最有效的方式打包您的值,因为您没有指定dtype
,它必须猜测如何存储它。当您的维度匹配时,明显首选是一个简单的矩阵。但是,当维度匹配时,我尝试将其强制转换为单元数组,但迄今为止未成功。 - engineerCdtype=object
的数组。 - askewchan