フィルターのクリア

Hi, is it possible to count the number of ones in a matrix ?

242 ビュー (過去 30 日間)
nafila aytija
nafila aytija 2016 年 4 月 8 日
コメント済み: Walter Roberson 2021 年 6 月 21 日
Example: Suppose we a have a matrix A=[1 0 0 1 0 0; 1 0 0 0 1 1]; how do I count the number of ones in this matrix??

採用された回答

Ahmet Cecen
Ahmet Cecen 2016 年 4 月 8 日
編集済み: Ahmet Cecen 2016 年 4 月 8 日
sum(A(:) == 1);
  6 件のコメント
Harsh Agarwal
Harsh Agarwal 2021 年 6 月 21 日
find(A==1)
Walter Roberson
Walter Roberson 2021 年 6 月 21 日
A = randi([0 1], 5, 5); A = xor(A,A.'); %random symmetric
A
A = 5×5 logical array
0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0
[r,c] = find(A);
mask = r>=c;
r = r(mask); c = c(mask);
[r,c]
ans = 4×2
4 1 5 1 5 2 5 3
or
[r,c] = find(tril(A))
r = 4×1
4 5 5 5
c = 4×1
1 1 2 3

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

その他の回答 (0 件)

カテゴリ

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