UNIQUE Unique
Section: Array Generation and Manipulations
Usage
Returns a vector containing the unique elements of an array. The first form is simplyy = unique(x)
where x
is either a numerical array or a cell-array of strings. The
result is sorted in increasing order. You can also retrieve two sets
of index vectors
[y, m, n] = unique(x)
such that y = x(m)
and x = y(n)
. If the argument x
is a matrix,
you can also indicate that FreeMat should look for unique rows in the
matrix via
y = unique(x,'rows')
and
[y, m, n] = unique(x,'rows')
Example
Here is an example in row mode--> A = randi(1,3*ones(15,3)) A = 1 1 1 3 2 2 1 3 3 2 2 1 1 3 1 2 3 1 2 2 3 1 2 2 3 3 3 2 1 1 2 1 1 2 3 2 1 1 2 2 3 1 3 3 2 --> unique(A,'rows') ans = 1 1 1 1 1 2 1 2 2 1 3 1 1 3 3 2 1 1 2 2 1 2 2 3 2 3 1 2 3 2 3 2 2 3 3 2 3 3 3 --> [b,m,n] = unique(A,'rows'); --> b ans = 1 1 1 1 1 2 1 2 2 1 3 1 1 3 3 2 1 1 2 2 1 2 2 3 2 3 1 2 3 2 3 2 2 3 3 2 3 3 3 --> A(m,:) ans = 1 1 1 1 1 2 1 2 2 1 3 1 1 3 3 2 1 1 2 2 1 2 2 3 2 3 1 2 3 2 3 2 2 3 3 2 3 3 3 --> b(n,:) ans = 1 1 1 3 2 2 1 3 3 2 2 1 1 3 1 2 3 1 2 2 3 1 2 2 3 3 3 2 1 1 2 1 1 2 3 2 1 1 2 2 3 1 3 3 2
Here is an example in vector mode
--> A = randi(1,5*ones(10,1)) A = 3 5 5 1 3 4 1 5 4 1 --> unique(A) ans = 1 3 4 5 --> [b,m,n] = unique(A,'rows'); --> b ans = 1 3 4 5 --> A(m) ans = 1 3 4 5 --> b(n) ans = 3 5 5 1 3 4 1 5 4 1
For cell arrays of strings.
--> A = {'hi','bye','good','tell','hi','bye'} A = [hi] [bye] [good] [tell] [hi] [bye] --> unique(A) ans = [bye] [good] [hi] [tell]