how to search about a matrix in a cell?

1 回表示 (過去 30 日間)
Sarah A
Sarah A 2019 年 1 月 19 日
コメント済み: Sarah A 2019 年 1 月 19 日
Hello,
Suppose that I have the cell matrix A which contains the following elements:
A={
[0;0] [0;1]
[1;0] [0;0]
}
So I want to use a line of code that can count how many [0;0] are in each row ?
Regards,

採用された回答

Guillaume
Guillaume 2019 年 1 月 19 日
編集済み: Guillaume 2019 年 1 月 19 日
it's always a good idea to give the result you expect for your example to avoid ambiguity.
A={
[0;0] [0;1]
[1;0] [0;0]
}
sum(cellfun(@(x) isequal(x, [0;0]), A), 2)
results in
ans =
1
1
count of [0;0] in each row.
  1 件のコメント
Sarah A
Sarah A 2019 年 1 月 19 日
Great ! Thank you for your answer.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 1 月 19 日
編集済み: madhan ravi 2019 年 1 月 19 日
Sarah's (OP) example:
A={ ...
[0;0] [0;1]
[1;0] [0;0]
};
counts=nnz(~cellfun(@any,A))
Gives:
counts =
2
Stephen's example:
A={[0;0] [0;1] [1;0] [0;0] [0;0]};
counts=nnz(~cellfun(@any,A))
Gives:
counts =
3
  5 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 19 日
check the edited answer
Sarah A
Sarah A 2019 年 1 月 19 日
Yes this code work with me:
A={ ...
[0;0] [0;1]
[1;0] [0;0]
};
counts=nnz(~cellfun(@any,A))
but what if I want to count how many [0;0] in each row? how many [0;1] in each row? how many [1;1] in each row?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by