Search code examples
matlabmatrixvectorunique

List all values in an array without repeats


Suppose I have a 100 x 100 matrix composed of some combination of 250s, 125s, 15s and 9s. I would like to return a sorted vector of all of the unique values in this matrix.

Something in the matter of:

sort(somefunction(matrix))=vector 

The result I would like to get is thus:

vector=9,15,125,250

Is there a quick and easy way to do this?


Solution

  • b = unique(a)
    

    Check the docs on unique

    A = randi(9,10,10);
    unique(A)
    ans =
         1
         2
         3
         4
         5
         6
         7
         8
         9