필터 지우기
필터 지우기

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
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