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