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

241 次查看(过去 30 天)
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 个评论
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 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