Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# unique

Unique values in array

## 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,setOrder) example
• [C,ia,ic] = unique(A,'rows',setOrder) example
• [C,ia,ic] = unique(A,'legacy') example
• [C,ia,ic] = unique(A,'rows','legacy') example
• [C,ia,ic] = unique(A,occurrence,'legacy') example
• [C,ia,ic] = unique(A,'rows',occurrence,'legacy') example

## Description

example

C = unique(A) returns the same data as in A, but with no repetitions.

• If A is a numeric array, logical array, character array, categorical array, or a cell array of strings, then unique returns the unique values in A. The values of C are in sorted order.

• If A is a table, then unique returns the unique rows in A. The rows of table C are in sorted order.

example

C = unique(A,'rows') treats each row of A as a single entity and returns the unique rows of A. The rows of the array C are in sorted order.

The 'rows' option does not support cell arrays.

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

• If A is a numeric array, logical array, character array, categorical array, or a cell array of strings, then C = A(ia) and A = C(ic).

• If A is a table, then 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,setOrder) and [C,ia,ic] = unique(A,'rows',setOrder) return 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. If no value is specified, the default is 'sorted'.

example

[C,ia,ic] = unique(A,'legacy'), [C,ia,ic] = unique(A,'rows','legacy'), [C,ia,ic] = unique(A,occurrence,'legacy'),and [C,ia,ic] = unique(A,'rows',occurrence,'legacy') preserve the behavior of the unique function from R2012b and prior releases.

The 'legacy' option does not support categorical arrays or tables.

## Examples

expand all

### Unique Values in Vector

Define a vector with a repeated value.

`A = [9 2 9 5];`

Find the unique values of A.

`C = unique(A)`
```C =

2     5     9```

### Unique Rows in Table

Define a table with repeated data.

```Name = {'Fred';'Betty';'Bob';'George';'Jane'};
Age = [38;43;38;40;38];
Height = [71;69;64;67;64];
Weight = [176;163;131;185;131];

A = table(Age,Height,Weight,'RowNames',Name)```
```A =

Age    Height    Weight
---    ------    ------
Fred      38     71        176
Betty     43     69        163
Bob       38     64        131
George    40     67        185
Jane      38     64        131   ```

Find the unique rows of A.

`C = unique(A)`
```C =

Age    Height    Weight
---    ------    ------
Bob       38     64        131
Fred      38     71        176
George    40     67        185
Betty     43     69        163   ```

unique returns the rows of A in sorted order by the first variable, Age and then by the second variable, Height.

### Unique Values and Their Indices

Define a vector with a repeated value.

`A = [9 2 9 5];`

Find the unique values of A and the index vectors ia and ic, such that C = A(ia) and A = C(ic).

`[C, ia, ic] = unique(A)`
```C =

2     5     9

ia =

2
4
1

ic =

3
1
3
2```

### Unique Rows in Matrix

Define a matrix with a repeated row.

`A = [9 2 9 5; 9 2 9 0; 9 2 9 5];`

Find the unique rows of A and the index vectors ia and ic, such that C = A(ia,:) and A = C(ic,:).

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

9     2     9     0
9     2     9     5

ia =

2
1

ic =

2
1
2```

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

### Unique Values in Vector with Specified 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 Values in Array Containing NaNs

Define a vector containing NaN.

`A = [5 5 NaN NaN];`

Find the unique values of A.

`C = unique(A)`
```C =

5   NaN   NaN```

unique treats NaN values as distinct.

### Unique Entries in Cell Array of Strings

Define a cell array of strings.

`A = {'one','two','twenty-two','One','two'};`

Find the unique strings contained in A.

`C = unique(A)`
```C =

'One'    'one'    'twenty-two'    'two'```

### Cell Array of Strings with Trailing White Space

Define a cell array of strings, A, where some of the strings have trailing white space.

`A = {'dog','cat','fish','horse','dog ','fish '};`

Find the unique strings contained in A.

`C = unique(A)`
```C =

'cat'    'dog'    'dog '    'fish'    'fish '    'horse'```

unique treats trailing white space in cell arrays of strings as distinct characters.

### Preserve Legacy Behavior of unique

Use the 'legacy' flag to preserve the behavior of unique from R2012b and prior releases in your code.

Find the unique elements of A with the current behavior.

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

2     5     9

ia1 =

2
4
1

ic1 =

3
1
3
2```

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

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

2     5     9

ia2 =

2     4     3

ic2 =

3     1     3     2```

## Input Arguments

expand all

### A — Input arraynumeric array | logical array | character array | categorical array | cell array of strings | table

Input array, specified as a numeric array, logical array, character array, categorical array, cell array of strings, or table.

If A is a table, unique does not take row names into account. Two rows that have the same values, but different names, are considered equal.

Furthermore, A can be an object with the following class methods:

• sort (or sortrows for the 'rows' option)

• ne

The object class methods must be consistent with each other. These objects include heterogeneous arrays derived from the same root class.

### setOrder — Order flag'sorted' (default) | '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 return in sorted order. For example: C = unique([5 5 3 4],'sorted') returns C = [3 4 5].
'stable'The values (or rows) in C return in the same order as in A. For example: C = unique([5 5 3 4],'stable') returns C = [5 3 4].

### occurrence — Occurrence flag for legacy behavior'last' (default) | 'first'

Occurrence flag for legacy behavior, specified as 'first' or 'last', indicates whether ia should contain the first or last indices to repeated values found in A.

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','legacy') 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','legacy') returns ia = 1.

## Output Arguments

expand all

### C — Unique data of Avector | matrix | table

Unique data of A, returned as a vector, matrix, or table. The following describes the shape of C when the input, A, is a vector or a matrix:

• If the 'rows' flag is not specified and A is a row vector, then C is a row vector.

• If the 'rows' flag is not specified and A is not a row vector, then C is a column vector.

• If the 'rows' flag is specified, then C is a matrix containing the unique rows of A.

The class of C is the same as the class of the input A.

### ia — Index to Acolumn vector

Index to A, returned as a column vector of indices to the first occurrence of repeated elements. When the 'legacy' flag is specified, ia is a row vector that contains indices to the last occurrence of repeated elements.

The indices satisfy C = A(ia) (or C = A(ia,:) for the 'rows' option).

### ic — Index to Ccolumn vector

Index to C, returned as a column vector when the 'legacy' flag is not specified. ic contains indices, such that A = C(ic) (or A = C(ic,:) for the 'rows' option).

## More About

expand all

### Tips

• To find unique rows in table A with respect to a subset of variables, you can use column subscripting. For example, you can use unique(A(:,vars)), where vars is a positive integer, a vector of positive integers, a variable name, a cell array of variable names, or a logical vector.

## See Also

Was this topic helpful?