필터 지우기
필터 지우기

find sum of all the entries with value 0

조회 수: 2 (최근 30일)
arun
arun 2013년 11월 29일
댓글: Jos (10584) 2013년 12월 2일
I have a cell matrix of mat = [ 1 0 0 4;0 2 0 4], i want to find the sum of the all the entries having value 0. in the case of given example the entries having count 0 are 4; so sum will be 4. how can i do this.

채택된 답변

Matt J
Matt J 2013년 11월 29일
편집: Matt J 2013년 11월 29일
  댓글 수: 3
Matt J
Matt J 2013년 11월 29일
편집: Matt J 2013년 11월 29일
No, what I proposed
sum(~mat(:))
works indepedently of ndims(mat) and will save you additional function calls to sum().
arun
arun 2013년 11월 30일
okieee,sorry............... i got it.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2013년 11월 30일
NNZ is the dedicated function to do this:
nnz(~mat)
  댓글 수: 3
John D'Errico
John D'Errico 2013년 11월 30일
Note that nnz(~mat) will be poor even for large sparse matrices, because if mat is sparse, then ~mat must create a large and rather dense logical matrix. Better then would be to use a form like:
numel(mat) - nnz(mat)
In my tests, for a large fairly sparse matrix like that generated by Matt in his comment, my form was the fastest.
Jos (10584)
Jos (10584) 2013년 12월 2일
Thanks, Matt and John, for the insights.

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

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by