JayDave
JayDave

Reputation: 115

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

Thus far, I have find(diff(A)==0), however this only proves useful if the elements are adjacent. I am wondering how identify whether or not a number occurs more than once within an array, regardless of position.

Thanks in advance for your time and help. :)

Upvotes: 1

Views: 6675

Answers (2)

bla
bla

Reputation: 26069

Another option is (given some matrix A):

length(unique(A))<length(A(:))

If any of the elements are the same the output of this line will be 1.

Upvotes: 2

mathematician1975
mathematician1975

Reputation: 21351

Try this. If A is your matrix

C = unique(A);

C will contain the unique elements of A so if the length is the same you have all unique elements. If you want to test a specific value trya

c = sum((A == value)

which will return the number of elements in A equal to value

Upvotes: 4

Related Questions