user3470496
user3470496

Reputation: 171

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?

Upvotes: 0

Views: 175

Answers (2)

Tony Tannous
Tony Tannous

Reputation: 473

b = sort(a(:));

This should do the work sort your matrix;

And this shall return all values into a vector.

b = unique(a(:));

Upvotes: -3

Adriaan
Adriaan

Reputation: 18177

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

Upvotes: 6

Related Questions