Skip to Main Content Skip to Search
Product Documentation

unique - Find unique values in array

In a future release, the behavior of unique will change. This change is introduced for adoption in R2012a. For a detailed demonstration that shows how you can preview the future behavior and preserve the current behavior of your existing code, see the example, Assessing the Impact of the Forthcoming Changes to unique. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.

Syntax

  • C = unique(A) example
  • C = unique(A,'rows') example
  • [C,ia,ic] = unique(A)
  • [C,ia,ic] = unique(A,'rows')
  • [C,ia,ic] = unique(A,occurrence) example
  • [C,ia,ic] = unique(A,'rows',occurrence) example

  • [C,ia,ic] = unique(___,'R2012a') example
  • [C,ia,ic] = unique(___,'legacy') example

  • [C,ia,ic] = unique(A,setOrder) example
  • [C,ia,ic] = unique(A,'rows',setOrder) example

Description

example

C = unique(A) for the array A, returns the same values as in A but with no repetitions. The values of C are in sorted order.

example

C = unique(A,'rows') for the matrix A, returns the unique rows of A. The rows of the matrix C are in sorted order.

[C,ia,ic] = unique(A) also returns index vectors ia and ic, such that C = A(ia) and A = C(ic).

[C,ia,ic] = unique(A,'rows') also returns index vectors ia and ic such that C = A(ia,:) and A = C(ic,:).

example

[C,ia,ic] = unique(A,occurrence) and [C,ia,ic] = unique(A,'rows',occurrence) specify which index is returned in ia when there are repeated values (or rows) in A. The default value is occurrence ='last', and unique returns the index of the last occurrence of each unique value (or row) in A. If occurrence='first', unique returns the index of the first occurrence of each unique value (or row).

example

[C,ia,ic] = unique(___,'R2012a') adopts the future behavior of the unique function. [C,ia,ic] = unique(___,'legacy') preserves the current behavior. The ___ symbol signifies that you can specify either flag, 'R2012a' or 'legacy', as the final argument with any previous syntax that accepts A, 'rows', or occurrence.

example

[C,ia,ic] = unique(A,setOrder) and [C,ia,ic] = unique(A,'rows',setOrder) returns C in a specific order. setOrder='sorted' returns the values (or rows) of C in sorted order. setOrder='stable' returns the values (or rows) of C in the same order as A.

Examples

Unique Values in a Vector

A = [9 2 9 5];
C = unique(A)
C =

     2     5     9

Find Unique Values and Their Indices

A = [9 2 9 5];
[C, ia, ic] = unique(A)
C =

     2     5     9


ia =

     2     4     3


ic =

     3     1     3     2

Unique Rows in a Matrix

A = [9 2 9 5; 9 2 9 0; 9 2 9 5];
[C, ia, ic] = unique(A,'rows')
C =

     9     2     9     0
     9     2     9     5


ia =

     2
     3


ic =

     2
     1
     2

Notice that the second row in A is identified as unique even though 9, 2, and 9 are repeated in the other rows.

Unique Values in a Vector with Specified Output Order

Use the setOrder argument to specify the ordering of the values in C.

Specify 'stable' if you want the values in C to have the same order as in A.

A = [9 2 9 5];
[C, ia, ic] = unique(A,'stable')
C =

     9     2     5


ia =

     1
     2
     4


ic =

     1
     2
     1
     3

Alternatively, you can specify 'sorted' order.

[C, ia, ic] = unique(A,'sorted')
C =

     2     5     9


ia =

     2
     4
     1


ic =

     3
     1
     3
     2

unique adopts the forthcoming behavior when you specify the setOrder argument. If you omit the setOrder argument, the output is sorted, but the size and content of C, ia, and ic conforms to the current behavior. Compare the sorted output here to the output from the example, Find Unique Values and Their Indices.

Unique Values in a Vector with Repeated Items

Specify whether ia should contain the first or last index to repeated values.

Specify the first occurrence of 9 in A.

A = [9 2 9 5];
[C1, ia1, ic1] = unique(A,'first')
C1 =

     2     5     9


ia1 =

     2     4     1


ic1 =

     3     1     3     2

Alternatively, you can specify the last occurrence of 9 in A.

A = [9 2 9 5];
[C2, ia2, ic2] = unique(A,'last')
C2 =

     2     5     9


ia2 =

     2     4     3


ic2 =

     3     1     3     2

Assessing the Impact of the Forthcoming Changes to unique

Use the 'R2012a' flag to assess the impact of the forthcoming behavior changes. Use the 'legacy' flag to preserve the current behavior of your existing code.

Find the unique elements of A with the current default behavior.

A = [9 2 9 5];
[C1, ia1, ic1] = unique(A)
C1 =

     2     5     9


ia1 =

     2     4     3


ic1 =

     3     1     3     2

Find the unique elements of A, and opt into the forthcoming behavior. In the future, this behavior will be the default.

[C2, ia2, ic2] = unique(A, 'R2012a')
C2 =

     2     5     9


ia2 =

     2
     4
     1


ic2 =

     3
     1
     3
     2

ia1 and ia2 have different shapes and content, while ic1 and ic2 differ only in shape.

Find the unique elements of A, and preserve the current behavior.

[C3, ia3, ic3] = unique(A, 'legacy')
C3 =

     2     5     9


ia3 =

     2     4     3


ic3 =

     3     1     3     2

C3, ia3, and ic3 match C1, ia1, and ic1 respectively.

Unique Values in Array Containing NaNs

A = [5 5 NaN NaN];
C = unique(A)
C =

     5   NaN   NaN

unique treats NaN values as distinct.

Input Arguments

A

Input Array
Vector | Matrix | N-D Array

Array in which to find unique values (or rows). A can be logical, char, a cell array of strings, or any numeric class. A also can be any object with the class methods: sort (or sortrows for the 'rows' option), and ne. This includes heterogeneous arrays derived from the same root class.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Support: Yes

'rows'

Rows Flag

Rows flag, specified as 'rows', identifies the rows of matrix A as individual elements. When you specify this flag, unique returns the unique rows of A. This option does not support cell arrays.

occurrence

Occurrence Flag
'last' | 'first'

Occurrence flag specifies whether ia should contain the first or last indices to repeated values found in C. The default value is 'last' in most cases. However, if you call unique with either the setOrder or 'R2012a' argument, the default occurrence is 'first'.

Occurrence FlagMeaning
'last'If there are repeated values (or rows) in A, then ia contains the index to the last occurrence of the repeated value. For example: [C,ia,ic] = unique([9 9 9],'last') returns ia = 3.
'first'If there are repeated values (or rows) in A, then ia contains the index to the first occurrence of the repeated value. For example: [C,ia,ic] = unique([9 9 9],'first') returns ia = 1.

setOrder

Order Flag
'sorted' | 'stable'

Order flag, specified as 'sorted' or 'stable', indicates the order of the values (or rows) in C.

Order FlagMeaning
'sorted'The values (or rows) in C, are returned in sorted order. For example: C = unique([5 5 3 4],'sorted') returns C = [3 4 5].
'stable'The values (or rows) in C, are returned in the same order as in A. For example: C = unique([5 5 3 4],'stable') returns C = [5 3 4].

'legacy'

Legacy Behavior Flag

Legacy behavior flag, when specified, preserves the current behavior. If the new behavior adversely affects your existing code, you can preserve the current behavior by specifying 'legacy' as the final argument.

'R2012a'

Future Behavior Flag

Future behavior flag, when specified, adopts the future behavior change. Specify 'R2012a' as the final argument to see what impact the new behavior has on your existing code.

Output Arguments

C

Unique Values of A
Vector | Matrix

Unique values of A returned as a vector or matrix. C is a vector, unless you specify the 'rows' flag. If 'rows' is specified, C is a matrix containing the unique rows of A.

ia

Index to A
Vector

Index to A is a vector that contains indices such that C = A(ia).

ic

Index to C
Vector

Index to C is a vector that contains indices such that A = C(ic).

See Also

intersect | ismember | issorted | setdiff | setxor | sort | union

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS