Is it possible: Split a structure?
조회 수: 53 (최근 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!