List all values in an array without repeats

2019-03-03 17:12发布

问题:

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?

回答1:

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


回答2:

b = sort(a(:));

This should do the work sort your matrix;

And this shall return all values into a vector.

b = unique(a(:));