Is it possible: Split a structure?

56 visualizaciones (últimos 30 días)
Zoe
Zoe el 15 de Jun. de 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!!!!!!!!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (3)

Fangjun Jiang
Fangjun Jiang el 15 de Jun. de 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 comentario
Zoe
Zoe el 15 de Jun. de 2011
Never too late. Thanks, very detailed and helpful:)

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 15 de Jun. de 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 comentario
Zoe
Zoe el 15 de Jun. de 2011
Yes, INDUSTRY_SECTOR is a cell array of strings. Tnahks :)

Iniciar sesión para comentar.


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

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by