How to do double strings match in single statement
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have below cell array matrix and I want check two strings matchig in single statement:
condition: If row element in column1 is Normal & column2 value>=20, then copy to normalData, else copy to abnormalData InputData:
Re/Attempt 12
Normal 24
Normal 13
Re/Attempt 26
Re/Attempt 28
Normal 23
Normal 12
Normal 15
normalData:
Normal 24
Normal 23
abnormalData:
Re/Attempt 12
Normal 13
Re/Attempt 26
Re/Attempt 28
Normal 12
Normal 15
Many thanks in advance,
0 comentarios
Respuestas (2)
Stephen23
el 28 de Abr. de 2017
Editada: Stephen23
el 28 de Abr. de 2017
C = {...
'Re/Attempt','12';...
'Normal','24';...
'Normal','13';...
'Re/Attempt','26';...
'Re/Attempt','28';...
'Normal','23';...
'Normal','12';...
'Normal','15'};
idx = strcmp('Normal',C(:,1)) & str2double(C(:,2))>20;
normalData = C(idx,:)
abnormalData = C(~idx,:)
1 comentario
dpb
el 28 de Abr. de 2017
Editada: dpb
el 28 de Abr. de 2017
If you use a cell array, that'll require cellfun
> isOK=~cellfun(@isempty,strfind(C(:,1),'Normal')) & [[C{:,2}]>=20].';
>> C(isOK,:)
ans =
'Normal' [24]
'Normal' [23]
>>
Other is just the complement of ~isOK of course.
This would be far easier to write if you would use a table and categorical variables.
NB: here I presumed the second cell is numeric not string...if were another string have to "wash, rinse, repeat" the strfind operation for each.
regexp is a little more friendly with cell strings but as noted, the table class and categorical variables are very useful for such data.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!