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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ


Translated by