Find row that has a cell containing all ones

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 16 Mag 2013
Modificato: Azzi Abdelmalek il 16 Mag 2013
[rows,col]=find(cellfun(@all ,logic))

Più risposte (1)

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

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by