Matlab struct数组转换为python [英] Matlab struct array to python
问题描述
我已经仔细研究了一下,试图找出一种在python中创建类似matlab的struct数组的方法.我在网上查看了一些问题,答案似乎没有帮助,或者我可能只是误解了它们,因为它们与我有关.因此,继续前进.我正在尝试形成一个等效于以下matlab代码的python.
I have thoroughly looked around to try figuring out a way to create a matlab like struct array in python. There are some questions online that I looked at and either the answers don't seem to help or I might simply be misinterpreting them as they pertain to me. So, moving on. I am trying to form a python equivalent to the following matlab code.
channel = [];
channel.PRN = 0;
channel.acquiredFreq = 0;
channel.codePhase = 0;
channel.status = '-';
channel = repmat(channel, 1, settings.numberOfChannels);
其中repmat基本上会创建一个名为channel的结构数组,其中的单元数等于settings.numberOfChannels,并且每个单元都有PRN,acquiredFreq等.
Where repmat would basically create a struct array called channel with a number of cells equal to settings.numberOfChannels and each of those would have PRN,acquiredFreq, etc.
稍后,我通过执行循环来更改这些值,从而访问此结构:
Later on, I access this struct by performing a loop that alters these values as such:
for ii = 1:settings.numberOfChannels
channel(ii).PRN = PRNindexes(ii);
channel(ii).acquiredFreq = acqResults.carrFreq(PRNindexes(ii));
channel(ii).codePhase = acqResults.codePhase(PRNindexes(ii));
我尝试了几种方法,但是在使用numpy的tile情况下(我可能一直使用不正确),它会吐出一些废话,或者当我尝试进行如下循环时:
I have tried several approaches but it either spits out nonsense in the case of tile using numpy(which i might have just been using incorrectly) or when I attempt to make a loop such as:
class test:
for iii in range(1,settings.numberOfChannels):
iii.PRN=0
iii.acquiredFreq=0
iii.codePhase=0
iii.status="-"
很有可能我认为这是语法错误或对python的误解,因为这是我第一次使用它.如果这是问这个问题或类似问题的不正确地点,我深表歉意.
More than likely I suppose it's a syntax error or my misunderstanding of python since this is my first time utilizing it. If this is the incorrect place to ask this or something of that nature, I apologize.
谢谢
推荐答案
更新:您可能想调查熊猫一个>.它的 与NumPy相比,Series和DataFrames更易于使用且功能更全 结构化数组.
Update: You may want to investigate Pandas. Its Series and DataFrames are easier to work with and more full-featured than NumPy structured arrays.
您可以使用NumPy 结构化数组:>
You could use a NumPy structured array:
import numpy as np
channel = np.zeros(1, dtype = [('PRN',int),
('acquiredFreq',int),
('codePhase',int),
('status','|S1')])
print(channel)
# [(0, 0, 0, '')]
按整数索引访问特定行:
Indexing by integer accesses a particular row:
print(channel[0])
# (0, 0, 0, '')
按列名索引将列作为数组返回:
Indexing by column name returns the column as an array:
print(channel['PRN'])
# [0]
或者您可以遍历每一行并分配给每个字段(列), 但这在NumPy中相对较慢.
Or you can loop through each row and assign to each field (column), but this is relatively slow in NumPy.
for row in channel:
row['PRN'] = 1
row['acquiredFreq'] = 1
row['codePhase'] = 1
row['status'] = '+'
print(channel)
# [(1, 1, 1, '+')]
为了完整起见,我还要提到您可以按行然后按列进行分配:
Just for completeness, I'll also mention you can assign by row then column:
channel[0]['status'] = '-'
print(channel)
# [(1, 1, 1, '-')]
或按列分配,然后按行分配:
or assign by column then row:
channel['PRN'][0] = 10
print(channel)
# [(10, 1, 1, '-')]
我展示了上面的内容,因为它与您发布的Matlab代码最相似.但是,让我再次强调,分配给NumPy数组中的单个单元很慢. NumPy执行上述操作的方法是执行整个数组分配:
I showed the above because it is most similar to the Matlab code you posted. However, let me stress again that assigning to individual cells in a NumPy array is slow. The NumPy-way to do the above is to do whole-array assignments instead:
channel['PRN'] = PRNindexes
其中PRNindexes
是一个序列(例如列表,元组或NumPy数组).
where PRNindexes
is a sequence (e.g. a list, tuple, or NumPy array).
您还可以使用花式索引(又称高级索引")以选择行:
You can also use fancy indexing (aka "advanced indexing") to select rows:
index = (channel.status == '+') # Select all rows with status '+'
channel['PRN'][index] = 10 # Set PRN to 10 for all those rows
请记住,精美索引会返回一个新数组,而不是原始数组的视图. (相反,基本切片"(例如channel[0]
或channel[1:10]
返回一个视图.)因此,如果要进行分配来更改原始数组的赋值,请先按列选择,然后按花式索引(index
)>
Just keep in mind that fancy indexing returns a new array, not a view of the original array. (In contrast, "basic slicing" (e.g. channel[0]
or channel[1:10]
returns a view.) So if you want to make assignments that alter the original array, select by column first, then fancy index (index
)
channel['PRN'][index] = ...
而不是
channel[index]['PRN'] = ...
这篇关于Matlab struct数组转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!