필터 지우기
필터 지우기

How can i sum cells that equals in matrix

조회 수: 1 (최근 30일)
Maxim Bragilovski
Maxim Bragilovski 2018년 1월 9일
댓글: Rik 2018년 1월 9일
if i have a matrix like that
2 2 2 2
1 1 1 1
0 0 1 0
how can i calculate the sum of rows that the values in all of their columns are equals.
for this example i want it will return 2

답변 (2개)

Roger Stafford
Roger Stafford 2018년 1월 9일
You can also use the 'all' function:
sum(all(diff(a,1,2)==0))
  댓글 수: 1
Rik
Rik 2018년 1월 9일
It might not matter at all, but I would like to point out that this is about twice as fast as my answer (at least on my machine on R2017b and R2012b).

댓글을 달려면 로그인하십시오.


Rik
Rik 2018년 1월 9일
a=[2 2 2 2
1 1 1 1
0 0 1 0];
sum(sum(abs(diff(a,1,2)),2)==0)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by