unique

Returns the elements of a set, without duplication.

Syntax

unique(x)

unique(x,'first')

unique(x,'last')

unique(x,'sorted')

unique(x,'stable')

unique(x,'rows')

[y,idx,idy] = unique(...)

Inputs

x
The set from which to remove duplicate items.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | vector | matrix
'last' (Default)
Returns idx with the highest indices for the items in x,
with 'sorted' implied for the output set.
Type: string
'first'
Returns idx with the lowest indices for the items in x,
with 'sorted' implied for the output set.
Type: string
'sorted'
Returns unique elements in ascending order, with 'last' implied for the idx output.
Type: string
'stable'
Returns unique elements in their original order, with 'first' implied for the idx output.
Type: string
'rows'
Returns unique rows as opposed to unique elements.
May be combined with the 'first' or 'last' options.
Type: string

Outputs

y
The unique elements of x.
idx
The vector of indices such that x(idx) = y.
idy
The vector of indices such that such that y(idy) = x.
This output is not yet available for cell inputs when 'stable' is specified.

Examples

Single return case:
y = unique([1,4,6,3,3,6,1,1])
y = [Matrix] 1 x 4
1  3  4  6
Multi-return case with first:
[y,idx,idy] = unique([1,4,6,3,3,6,1,1],'first')
 = [Matrix] 1 x 4
1  3  4  6
idx = [Matrix] 1 x 4
1  4  2  3
idy = [Matrix] 1 x 8
1  3  4  2  2  4  1  1
Multi-return case with rows:
[y,idx,idy] = unique([1,4,6;3,3,6;8,6,9;3,3,6], 'rows')
y = [Matrix] 3 x 3
1  4  6
3  3  6
8  6  9
idx = [Matrix] 3 x 1
1
4
3
idy = [Matrix] 4 x 1
1
2
3
2