Hi, I have a array like below: [0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17]
I want count how many numbers >0 in the array (like in this case count is 3 (because 10,15,&17). Can some please help me how to count this in matlab. Thanks in advance

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 20 日

1 投票

If your array doesn't contain negative numbers
a=[0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17]
out=nnz(a)

2 件のコメント

Shoaibur Rahman
Shoaibur Rahman 2014 年 12 月 20 日
nnz will count the number of all non-zero elements in the array, including negatives. So, this function would be a better choice if you want to do so.
Mekala balaji
Mekala balaji 2014 年 12 月 20 日
Great Thank you sir

サインインしてコメントする。

その他の回答 (2 件)

Shoaibur Rahman
Shoaibur Rahman 2014 年 12 月 20 日

2 投票

A=[0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17];
sum(A>0)
Javaid Iqbal
Javaid Iqbal 2018 年 12 月 3 日

1 投票

n=numel(A);

1 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 3 日
no this would give you total number of elements in the array

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by