Reputation: 1271
Currently to perform multiple queries using find
, I invoke each query individually separated by |
index = find(strcmp(data{:,{'type'}},'A') | strcmp(data{:,{'type'}},'B') | strcmp(data{:,{'type'}},'C') | strcmp(data{:,{'type'}},'D'));
To find all rows that where the field 'type' contains either A, B, C or D.
data
is held in a table hence the usage of }
.
Is there a more concise way of doing this without the need to specify the query in full each time?
Upvotes: 0
Views: 208
Reputation: 47402
You could use ismember
instead of multiple uses of strcmp
.
index = find(ismember(data{:,{'type'}}, {'A','B','C','D'}));
An alternative (because ismember
will probably be slower than multiple uses of strcmp
) would be to factor out the repeated code -
x = data{:, {'type'}}; %# This isn't valid MATLAB but whatever...
index = find(strcmp(x,'A') | strcmp(x,'B') | strcmp(x,'C') | strcmp(x,'D'));
You could also use multiple lines for readability
x = data{:, {'type'}}; %# This isn't valid MATLAB but whatever...
index = find(strcmp(x,'A') ...
| strcmp(x,'B') ...
| strcmp(x,'C') ...
| strcmp(x,'D'));
Upvotes: 1