How to count number of nonzero elements in part of cell arrays?

3 ビュー (過去 30 日間)
Nik Rocky
Nik Rocky 2020 年 6 月 16 日
コメント済み: Nik Rocky 2020 年 6 月 16 日
Hello,
I have a cell array C 1x5 cells with 1x3001 doubles
I want to count amount of elements bigger than zero in C{1,2}, C{1,3}, C{1,4} and C{1,5} (without C{1,1})
I can do:
numel(nonzeros(C{:,2})) + numel(nonzeros(C{:,3})) + .....
or in a loop:
for i = 2:5
Badumpsss = Badumpsss+numel(nonzeros(C{:,i}));
end
But maybe there are better way?

採用された回答

David Hill
David Hill 2020 年 6 月 16 日
sum(arrayfun(@(n)nnz(C{1,n}>0),2:5));
  3 件のコメント
David Hill
David Hill 2020 年 6 月 16 日
sum(arrayfun(@(n)nnz(C{1,n}>=0),2:5));
Nik Rocky
Nik Rocky 2020 年 6 月 16 日
Facepalm, thank you!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by