python中复杂的类似matlab的数据结构(numpy/scipy) [英] Complex matlab-like data structure in python (numpy/scipy)

查看:68
本文介绍了python中复杂的类似matlab的数据结构(numpy/scipy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have data currently structured as following in Matlab

item{i}.attribute1(2,j)

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

Where item is a cell from i = 1 .. n each containing the data structure of multiple attributes each a matrix of size 2,j where j = 1 .. m. The number of attributes is not fixed.

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

I have to translate this data structure to python, but I am new to numpy and python lists. What is the best way of structuring this data in python with numpy/scipy?

谢谢.

推荐答案

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

I've often seen the following conversion approaches:

matlab 数组 -> python numpy 数组

matlab array -> python numpy array

matlab 元胞数组 -> python 列表

matlab cell array -> python list

matlab 结构 -> python 字典

matlab structure -> python dict

因此,在您的情况下,这将对应于包含字典的 python 列表,这些字典本身包含 numpy 数组作为条目

So in your case that would correspond to a python list containing dicts, which themselves contain numpy arrays as entries

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

注意

不要忘记python中的0-indexing!

Don't forget the 0-indexing in python!

[更新]

附加:类的使用

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

Further to the simple conversion given above, you could also define a dummy class, e.g.

class structtype():
    pass

这允许以下类型的使用:

This allows the following type of usage:

>> 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

在这种情况下,您的示例变为,例如

Your example in this case becomes, e.g.

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

这篇关于python中复杂的类似matlab的数据结构(numpy/scipy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆