matlab使用python返回数组_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);

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

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

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.

Thank you

解决方案

Update: You may want to investigate Pandas. Its

Series and DataFrames are easier to work with and more full-featured than NumPy

structured arrays.

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]

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, '-')]

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

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

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] = ...

rather than

channel[index]['PRN'] = ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值