How to check number of zeros in an array?

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

 Accepted Answer

Paolo
Paolo on 6 Jul 2018
Edited: Paolo on 6 Jul 2018
A = nnz(~A)
B = nnz(~B)
C = nnz(~C)
D = nnz(~D)

1 Comment

I doubt that it gets much more compact than that!

Sign in to comment.

More Answers (1)

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.

Categories

Find more on Elementary Math in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!