Reputation: 10176
Assuming I've got a cell Array like this:
1x25 double 1x25 double 1x25 double 1x11 double 'Mean'
1000 0 0 5 0
1000 10 5 100 0
1000 20 5 200 0
1000 30 5 100 0
And now I'd like to check if there is ANY row in this cell array where the first column is 1000
, the second column is 30
and the third column is 5
and the fourth column is 100
- how to do that? The first row should be neglected as there are only column headers in it.
I came up with quite a strange approach:
sum(sum([[data{2:end, 1}]'==1000 [data{2:end, 2}]'==30 [data{2:end, 3}]'==5 [data{2:end, 4}]'==100], 2)==4)
Whereas the result has to be > 0
so that there is at least one occurrence of data-tupel...
Thanks in advance
EDIT: I came up with:
found = ismember([1000 30 5 100], cell2mat(data(2:end, 1:4)), 'rows')
I think this solution is quite good - but if you've got an even better approach, just post it. It doesn't need to be fast, just has to be readable and simple.
Upvotes: 1
Views: 1039
Reputation: 112659
This is perhaps simpler:
all(bsxfun(@eq, cell2mat(data(2:end,1:4)), [1000 30 5 100]),2)
Another possibility (probably slower):
arrayfun(@(n) all([data{n,1:4}]==[1000 30 5 100]),(2:size(data,1)).')
Upvotes: 1