Find row that has a cell containing all ones

3 views (last 30 days)
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.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 16 May 2013
Edited: Azzi Abdelmalek on 16 May 2013
[rows,col]=find(cellfun(@all ,logic))

More Answers (1)

Jan
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})];

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!