Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all of the elements in an array are the same, in matlab?

Tags:

matlab

There must be a simple matlab way of doing this. I have a row vector and I want to check if all of the elements are equal. Brute forcing this in a loop is easy, but looking for a more efficient approach :) The elements are integers

like image 338
zebra Avatar asked Apr 24 '12 03:04

zebra


People also ask

How do you check if all array elements are the same?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.

How do you check if two values in an array are equal in MATLAB?

tf = isequal( A,B ) returns logical 1 ( true ) if A and B are equivalent; otherwise, it returns logical 0 ( false ).


2 Answers

I think it can be as simple as

if all(v == v(1)) 

Another method would be

if range(v) == 0 
like image 127
Ben Voigt Avatar answered Oct 06 '22 12:10

Ben Voigt


Another solution:

numel(unique(v))==1 
like image 29
Andrey Rubshtein Avatar answered Oct 06 '22 13:10

Andrey Rubshtein