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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by