MATLAB Function Reference |
Find unique elements of vector
Syntax
Description
b = unique(A)
returns the same values as in A
but with no repetitions. The resulting vector is sorted in ascending order. A
can be a cell array of strings.
b = unique(A, 'rows')
returns the unique rows of A
.
[b, m, n] = unique(...)
also returns index vectors m
and n
such that b = A(m)
and A = b(n)
. Each element of m
is the greatest subscript such that b = A(m)
. For row combinations, b = A(m,:)
and A = b(n,:)
.
Examples
A = [1 1 5 6 2 3 3 9 8 6 2 4] A = 1 1 5 6 2 3 3 9 8 6 2 4 [b, m, n] = unique(A) b = 1 2 3 4 5 6 8 9 m = 2 11 7 12 3 10 9 8 n = 1 1 5 6 2 3 3 8 7 6 2 4 A(m) ans = 1 2 3 4 5 6 8 9 b(n) ans = 1 1 5 6 2 3 3 9 8 6 2 4
Because NaN
s are not equal to each other, unique
treats them as unique elements.
See Also
intersect
, ismember
, issorted
, setdiff
, setxor
, union
union | unix |
© 1994-2005 The MathWorks, Inc.