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

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

问题描述

我目前在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索引!

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(numpy/scipy)中类似于matlab的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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