How to filter data from table using multiple strings
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
André Kanji
el 8 de Mayo de 2017
Comentada: Peter Perkins
el 9 de Mayo de 2017
I have a table(60000 by 20) in which one of the columns have the following codes(extracted data):
c={'EGGD';'CSED';'CSED';'CSED';'CSED';'AVVT';'LBEB';'EGGD';'LBEB'}
And I just want the data that correspond to 'CSED' and 'AVVT'(example), there are many more.
One of my solutions:
cell=table2cell(table)
rowsCSED=any(strcmp(cell,'CSED'),2);
rowsAVVT=any(strcmp(cell,'AVVT'),2);
rows=rowsCSED | rowsAVVT;
table(rows,:)
Is there any way to do this without reverting to a loop?
0 comentarios
Respuesta aceptada
dpb
el 8 de Mayo de 2017
Editada: dpb
el 8 de Mayo de 2017
>> t=table(categorical(c),'variablenames',{'Code'}); % put your data back into the table from whence it came
>> summary(t)
Variables:
Code: 9x1 categorical
Values:
AVVT 1
CSED 4
EGGD 2
LBEB 2
>> t(ismember(t.Code,{'CSED','AVVT'}),:) % look up the matching rows...
ans =
Code
____
CSED
CSED
CSED
CSED
AVVT
>>
NB: Such things work much better if you make the codings categorical variables rather than leaving as string data.
1 comentario
Peter Perkins
el 9 de Mayo de 2017
Using categorical becomes especially readable in the simpler case of finding only one category:
t(t.Code == 'CSED',:)
With two categories, you could use
t(t.Code=='CSED' | t.code =='AVVT',:)
but at some point, you're better off with ismember.
Más respuestas (0)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!