How to check number of zeros in an array?

131 ビュー (過去 30 日間)
Aswin Sandirakumaran
Aswin Sandirakumaran 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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by