Is it possible: Split a structure?

53 Ansichten (letzte 30 Tage)
Zoe
Zoe am 15 Jun. 2011
I have a structure, Memb
Memb =
NAME: {963x1 cell}
INDUSTRY_SECTOR: {963x1 cell}
PRICE: [963x1 double]
VOLUME: [963x1 double]
Memb is already sorted by INDUSTRY_SECTOR (totally 9 sectors, Eneragy, Finance...).
Now I'd like to split the structure Memb into 9 individual structures according to which sector they belones to. Is it possible? Could any one provide a simple way to code?? (Avoid using loop if possible)
Any suggestions are greatly appreciate. Thanks a lot!!!!!!!!

Akzeptierte Antwort

the cyclist
the cyclist am 15 Jun. 2011
>> [isInOneOfTheListedSectors,indexToWhichSector] = ismember([Memb.INDUSTRY_SECTOR],{'Energy';'Finance'});
  1 Kommentar
Zoe
Zoe am 15 Jun. 2011
Yes, this works well. Thanks:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Fangjun Jiang
Fangjun Jiang am 15 Jun. 2011
Not sure if it is too late but it would be easier for coding if your data is stored in a structure array, instead of a structure. For example:
Memb =
NAME: {'a' 'b' 'c'}
INDUSTRY: {'ENG' 'ENG' 'FIN'}
PRICE: [1 2 3]
VOLUMN: [100 200 300]
Do the following:
S=struct('Name',Memb.NAME,'INDUSTRY',Memb.INDUSTRY,'PRICE',mat2cell(Memb.PRICE,1,ones(1,3)),'VOLUMN',mat2cell(Memb.VOLUMN,1,ones(1,3)))
You'll get
S =
1x3 struct array with fields:
Name
INDUSTRY
PRICE
VOLUMN
>> S(1)
ans =
Name: 'a'
INDUSTRY: 'ENG'
PRICE: 1
VOLUMN: 100
Then
IndustryName=unique({S.INDUSTRY})
ENG_index=ismember({S.INDUSTRY},'ENG')
ENG_data=S(ENG_index)
ENG_data =
1x2 struct array with fields:
Name
INDUSTRY
PRICE
VOLUMN
Note that in my example, Memb.NAME is a row vector. Your Memb.NAME is a column vector. So if you decide to convert to structure array, you need to use:
S=struct('Name',Memb.NAME,'INDUSTRY_SECTOR',Memb.INDUSTRY_SECTOR,'PRICE',mat2cell(Memb.PRICE,ones(963,1),1),'VOLUMN',mat2cell(Memb.VOLUMN,ones(963,1),1))
  1 Kommentar
Zoe
Zoe am 15 Jun. 2011
Never too late. Thanks, very detailed and helpful:)

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 15 Jun. 2011
INDUSTRY_SECTOR is a cell array of strings? If so, then ismember() against the valid list; the second output of ismember will be the index of the sector. After that probably the easiest would be a loop over the number of sectors to do the actual splitting.
  1 Kommentar
Zoe
Zoe am 15 Jun. 2011
Yes, INDUSTRY_SECTOR is a cell array of strings. Tnahks :)

Melden Sie sich an, um zu kommentieren.


Zoe
Zoe am 15 Jun. 2011
xInds = strcmp(Memb.INDUSTRY_SECTOR,'Basic Materials');
That works too!

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by