unique

Returns the unique elements in the input x

Syntax

unique(x)

unique(x,'rows')

unique(x,'first')

unique(x,'last')

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

Inputs

x
Any valid scalar | vector | matrix.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | vector | matrix
rows
Specifies to search for common rows as opposed to common elements.
Type: string
Dimension: string
last
Default option. Returns the highest possible indices in x.
Type: string
Dimension: string
first
Default option. Returns the lowest possible indices in x.
Type: string
Dimension: string

Outputs

R
The unique elements in input x.
idx
The idx vector relates the values in Y to the corresponding index in x such that x(idx)=R
idy
The idy vector relates the values in x to the corresponding index in Y such that R(idy)=x

Examples

Simple example:
unique([1;4;6;3;3;6;1;1])
R = [ 1 ; 3 ; 4 ; 6 ]
Simple example with rows:
[R,idx,idy]=unique([1;4;6;3;3;6;1;1], 'rows')
R = [ 1 ; 3 ; 4 ; 6 ]
idx = [ 8 ; 5 ; 2 ; 6 ]
idy = [ 1 ; 3 ; 4 ; 2 ; 2 ; 4 ; 1 ; 1 ]
Simple example with rows:
[R,idx,idy]=unique([1;4;6;3;3;6;1;1],'first')
R = [ 1 ; 3 ; 4 ; 6 ]
idx = [ 1 ; 4 ; 2 ; 3 ]
idy = [ 1 ; 3 ; 4 ; 2 ; 2 ; 4 ; 1 ; 1 ]