Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all rows of a matrix are equal [duplicate]

What's the most efficient way of checking if all rows of a matrix are numerically equal? Preferably looking for something without a for loop.

like image 941
James Avatar asked Sep 14 '25 08:09

James


1 Answers

Find difference between consecutive elements across columns with diff(..,1) and then detect if all such differences are zeros with nnz(..)==0, giving us a easy-peasy-chessy one-liner and pretty efficient solution -

isallrowsequal = nnz(diff(A,1))==0                      %// A is input matrix
like image 149
Divakar Avatar answered Sep 17 '25 07:09

Divakar