필터 지우기
필터 지우기

Counting number of nonempty cells within a cell

조회 수: 39 (최근 30일)
Julia Gorman
Julia Gorman 2022년 7월 26일
댓글: Walter Roberson 2022년 7월 26일
I have a 32x3 cell and I want to count the number of nonempty cells within it. When I use N = nnz(my_cell), this is the error I get
Undefined function 'nnz' for input arguments of type 'cell'.

채택된 답변

Walter Roberson
Walter Roberson 2022년 7월 26일
nnz(~cellfun(@isempty, my_cell))

추가 답변 (1개)

David Hill
David Hill 2022년 7월 26일
nnz(cell2mat(my_cell));%assuming each cell is a number
  댓글 수: 3
David Hill
David Hill 2022년 7월 26일
Attach your cell array.
for k=1:32;
for m=1:3;
a{k,m}=randi(30,1)-1;%a is a 32x3 cell array of numbers
end
end
N=nnz(cell2mat(a))
N = 94
Walter Roberson
Walter Roberson 2022년 7월 26일
The question is not about nonzero cells, it is about nonempty cells, in a context where cells might contain different number of entries each. For example, length 5, length 0, length 3

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by