How to do double strings match in single statement
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Stephen23
am 28 Apr. 2017
Bearbeitet: Stephen23
am 28 Apr. 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 Kommentar
dpb
am 28 Apr. 2017
Bearbeitet: dpb
am 28 Apr. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!