Is it possible: Split a structure?
56 ビュー (過去 30 日間)
古いコメントを表示
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!!!!!!!!
0 件のコメント
採用された回答
the cyclist
2011 年 6 月 15 日
>> [isInOneOfTheListedSectors,indexToWhichSector] = ismember([Memb.INDUSTRY_SECTOR],{'Energy';'Finance'});
その他の回答 (3 件)
Fangjun Jiang
2011 年 6 月 15 日
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))
Walter Roberson
2011 年 6 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!