python中的复杂类似matlab的数据结构(numpy/scipy)

Sky*_*yNT 12 python matlab numpy

我的数据目前在Matlab中的结构如下

item{i}.attribute1(2,j)
Run Code Online (Sandbox Code Playgroud)

其中item是来自i = 1 .. n的单元,每个包含多个属性的数据结构,每个属性都是大小为2的矩阵,其中j = 1 ... m.属性数量不固定.

我必须将此数据结构转换为python,但我是numpy和python列表的新手.使用numpy/scipy在python中构造此数据的最佳方法是什么?

谢谢.

jme*_*etz 20

我经常看到以下转换方法:

matlab数组 - > python numpy数组

matlab单元格数组 - > python列表

matlab结构 - > python dict

所以在你的情况下,这将对应于包含dicts的python列表,它们本身包含numpy数组作为条目

item[i]['attribute1'][2,j]

注意

不要忘记python中的0索引!

[更新]

附加:使用课程

除了上面给出的简单转换之外,您还可以定义一个虚拟类,例如

class structtype():
    pass
Run Code Online (Sandbox Code Playgroud)

这允许以下类型的用法:

>> 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
Run Code Online (Sandbox Code Playgroud)

在这种情况下你的例子变成,例如

>> item = [ structtype() for i in range(10)]
>> item[9].a = numpy.array([1,2,3])
>> item[9].a[1]
2
Run Code Online (Sandbox Code Playgroud)