Find row that has a cell containing all ones

3 Ansichten (letzte 30 Tage)
Harold
Harold am 16 Mai 2013
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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 16 Mai 2013
Bearbeitet: Azzi Abdelmalek am 16 Mai 2013
[rows,col]=find(cellfun(@all ,logic))
  1 Kommentar
Harold
Harold am 16 Mai 2013
Nice. I don't know why I didn't think of that. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by