【发布时间】:2017-05-09 16:26:18
【问题描述】:
我正在使用scipy.io 导入一个matlab 文件,并试图找到它的尺寸。看来,即使文件被加载到 python 中,它也无法给出尺寸。这是为什么?以及如何解决这个问题?
>>> import scipy.io
>>> pref_mat = scipy.io.loadmat('pref_mat_loc.mat')
>>> R=pref_mat
>>> import numpy
>>> R=numpy.array(R)
>>> len(R)
0
>>> R # We see that the first line of the file is getting printed, means the file has been loaded.
array({'pref_mat': array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint16), '__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon May 08 23:42:05 2017', '__version__': '1.0', '__globals__': []}, dtype=object)
>>> len(R.shape) # But it appears here as though R is empty
0
>>> R.shape # As does here
()
【问题讨论】:
-
len(R.shape)与R.ndim相同,以供将来参考。 -
R=pref_mat的目的是什么?为什么不只是R = scipy.io.loadmat(...)? -
为什么以...的名义你在做
R=numpy.array(R)?你明白loadmat返回的是什么吗? -
@MadPhysicist 为什么?发生了什么?我不是普通的 python 用户,我只是碰巧需要它来完成这项特定的工作。据我了解,
loadmat加载 matlab 文件;我不知道它会返回什么。是字典吗? -
确实如此。看看你自己的输出和docs
标签: python arrays matlab numpy