Find row that has a cell containing all ones

3 ビュー (過去 30 日間)
Harold
Harold 2013 年 5 月 16 日
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.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 16 日
編集済み: Azzi Abdelmalek 2013 年 5 月 16 日
[rows,col]=find(cellfun(@all ,logic))
  1 件のコメント
Harold
Harold 2013 年 5 月 16 日
Nice. I don't know why I didn't think of that. Thanks.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2013 年 5 月 17 日
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})];
  1 件のコメント
Harold
Harold 2013 年 5 月 17 日
Thanks Jan, I'll try this also.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by