Find row that has a cell containing all ones
3 views (last 30 days)
Show older comments
Suppose I have a cell array as follows,
B = {[3,2.5,1],[3,-1,3.5];
[3,-1,3.5],[3,2.5,2];
[3,4,-1],[3,3,0.25]};
I find the cells that are the same calling the anonymous function in a for loop. For testing purposes I'm only considering the first two rows of B.
is_same = @(x,y) cellfun(@eq, x, y,'UniformOutput',0);
key = [is_same(B(1, 1), B(2, 1)),...
is_same(B(1, 1), B(2, 2));...
is_same(B(1, 2), B(2, 1)),...
is_same(B(1, 2), B(2, 2))];
This gives me
logic = {[1, 0, 0],[1, 1, 0];...
[1, 1, 1],[1, 0, 0]};
I now want to find the row in logic, where a cell contains all ones? I want to be able to use this row indice in B to retrieve the row. With this retrieved row, I will apply the same steps above for other rows in B. For now, I just want to get the row indice problem solved.
0 Comments
Accepted Answer
Azzi Abdelmalek
on 16 May 2013
Edited: Azzi Abdelmalek
on 16 May 2013
[rows,col]=find(cellfun(@all ,logic))
More Answers (1)
Jan
on 17 May 2013
Note: This would be much faster and simpler without anonymous and cellfuns:
key = [isqual(B{1, 1}, B{2, 1}),...
isqual(B{1, 1}, B{2, 2});...
isqual(B{1, 2}, B{2, 1}),...
isqual(B{1, 2}, B{2, 2})];
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!