我目前在 Matlab 中的数据结构如下
item{i}.attribute1(2,j)
其中 item 是 i = 1 .. n 中的一个单元格,每个单元格包含多个属性的数据结构,每个属性的大小为 2,j 的矩阵,其中 j = 1 .. m。属性的数量不固定。
我必须将此数据结构转换为 python,但我对 numpy 和 python 列表很陌生。使用 numpy/scipy 在 python 中构造这些数据的最佳方法是什么?
Thanks.
我经常看到以下转换方法:
matlab 数组 -> python numpy 数组
matlab 元胞数组 -> python 列表
matlab结构-> python dict
因此,在您的情况下,这将对应于包含字典的 python 列表,字典本身包含 numpy 数组作为条目
item[i]['attribute1'][2,j]
Note
不要忘记 python 中的 0 索引!
[Update]
附加:使用类
除了上面给出的简单转换之外,您还可以定义一个虚拟类,例如
class structtype():
pass
这允许以下类型的使用:
>> s1 = structtype()
>> print s1.a
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-7734865fddd4> in <module>()
----> 1 print s1.a
AttributeError: structtype instance has no attribute 'a'
>> s1.a=10
>> print s1.a
10
在这种情况下,你的例子变成了,例如
>> item = [ structtype() for i in range(10)]
>> item[9].a = numpy.array([1,2,3])
>> item[9].a[1]
2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)