How to check number of zeros in an array?

조회 수: 123 (최근 30일)
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 7월 6일
답변: Raul Andres Lopez Romero 2018년 7월 6일
Eg: I have a vector A = [0,0,4,3] and B = [0,1,2,3,4] and C = [0,0,0,0,0] and D = [0,1,2,0,0]
SO THE OUTPUT SHOULD CONTAIN NUMBER OF ZEROS IN EACH VECTOR:
A = 2
B = 1
C = 5
D = 3

채택된 답변

Paolo
Paolo 2018년 7월 6일
편집: Paolo 2018년 7월 6일
A = nnz(~A)
B = nnz(~B)
C = nnz(~C)
D = nnz(~D)
  댓글 수: 1
Stephen23
Stephen23 2018년 7월 6일
I doubt that it gets much more compact than that!

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

추가 답변 (1개)

Raul Andres Lopez Romero
Raul Andres Lopez Romero 2018년 7월 6일
Try this:
nzeros=numel(A)-nnz(A)
numel(X) give you the number of elements in an array, nnz(X) give you the numbers of Non zero elements in an array.
try it for every array you have.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by