How to do double strings match in single statement

1 次查看(过去 30 天)
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,

回答(2 个)

Stephen23
Stephen23 2017-4-28
编辑:Stephen23 2017-4-28
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,:)

dpb
dpb 2017-4-28
编辑:dpb 2017-4-28
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by