How to do double strings match in single statement

1 vue (au cours des 30 derniers jours)
Kanakaiah Jakkula
Kanakaiah Jakkula le 28 Avr 2017
Modifié(e) : dpb le 28 Avr 2017
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,

Réponses (2)

Stephen23
Stephen23 le 28 Avr 2017
Modifié(e) : Stephen23 le 28 Avr 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 commentaire
KL
KL le 28 Avr 2017
I was writing something similar but yours is neat and clean, voted!

Connectez-vous pour commenter.


dpb
dpb le 28 Avr 2017
Modifié(e) : dpb le 28 Avr 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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by