Description

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

If A is a table or timetable, then
unique returns the unique rows in
A in sorted order. For timetables,
unique takes row times and row values into
account when determining whether rows are unique, and sorts the
output timetable C by row times.

C = unique(A,___,'rows')
and
C = unique(A,'rows',___)
treat each row of A as a single entity and return the
unique rows of A in sorted order. You must specify
A and optionally can specify setOrder
or occurrence.

The 'rows' option does not support cell arrays.

[C,ia,ic] = unique(___)
also returns index vectors ia and ic
using any of the previous syntaxes.

Count of Unique Elements

Find the unique elements in a vector and then use accumarray to count the number of times each unique element appears.

Create a vector of random integers from 1 through 5.

a = randi([1 5],200,1);

Find the unique elements in the vector. Return the index vectors ia and ic.

[C,ia,ic] = unique(a);

Count the number of times each element in C appears in a. Specify ic as the first input to accumarray and 1 as the second input so that the function counts repeated subscripts in ic. Summarize the results.

Unique Elements in Presence of Numerical Error

Create a vector x. Obtain a second vector y by transforming and untransforming x. This transformation introduces round-off differences in y.

x = (1:6)'*pi;
y = 10.^log10(x);

Verify that x and y are not identical by taking the difference.

x-y

ans = 6×1
10-14 ×
0.0444
0
0
0
0
-0.3553

Use unique to find the unique elements in the concatenated vector [x;y]. The unique function performs exact comparisons and determines that some values in x are not exactly equal to values in y. These are the same elements that have a nonzero difference in x-y. Thus, c contains values that appear to be duplicates.

c = unique([x;y])

c = 8×1
3.1416
3.1416
6.2832
9.4248
12.5664
15.7080
18.8496
18.8496

Use uniquetol to perform the comparison using a small tolerance. uniquetol treats elements that are within tolerance as equal.

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].

Data Types: char | string

occurrence — Occurrence flag'first' (default) | 'last'

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

Occurrence Flag

Meaning

'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. This is the default behavior when the
'legacy' flag is specified.

'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.
This is the default behavior.

Output Arguments

C — Unique data of Aarray

Unique data of A, returned as an array. The class of
C is the same as the class of the input
A. The shape of C depends on
whether the input 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.

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 generally satisfy C = A(ia).
If A is a table, or if the 'rows' option
is specified, then C = A(ia,:).

ic — Index to Ccolumn vector

Index to C, returned as a column vector
when the 'legacy' flag is not specified. ic contains
indices that satisfy the following properties.

If A is a vector, then A
= C(ic).

If A is a matrix or array, then A(:)
= C(ic).

If A is a table, or if the 'rows' option
is specified, then A = C(ic,:).

Tips

Use uniquetol to find unique floating-point numbers using
a tolerance.

To find unique rows in tables or timetables 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.
Alternatively, you can use vartype to create a subscript
that selects variables of a specified type.

Extended Capabilities

Tall ArraysCalculate with arrays that have more rows than fit in memory.