unique - Unique values - MATLAB (original) (raw)
Syntax
Description
[C](#bs%5F6vpd-1-C) = unique([A](#bs%5F6vpd-1-A))
returns the same data as in A
, but with no repetitions.C
is in sorted order.
- If
A
is a table or timetable, thenunique
returns the unique rows inA
in sorted order. For timetables,unique
takes row times and row values into account when determining whether rows are unique, and sorts the output timetableC
by row times. - If
A
is a categorical array, then the sort order is determined by the order of the categories.
[C](#bs%5F6vpd-1-C) = unique([A](#bs%5F6vpd-1-A),[setOrder](#bs%5F6vpd-1-setOrder))
returns the unique values of A
in a specific order.setOrder
can be 'sorted'
(default) or'stable'
.
[C](#bs%5F6vpd-1-C) = unique([A](#bs%5F6vpd-1-A),[occurrence](#bs%5F6vpd-1-occurrence))
specifies which indices to return in case of repeated values.occurrence
can be 'first'
(default) or'last'
.
[C](#bs%5F6vpd-1-C) = unique([A](#bs%5F6vpd-1-A),[setOrder](#bs%5F6vpd-1-setOrder),[occurrence](#bs%5F6vpd-1-occurrence))
specifies both the order and which indices to return in case of repeated values. (since R2025a)
[C](#bs%5F6vpd-1-C) = unique([A](#bs%5F6vpd-1-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 specifyA
and optionally can specify setOrder
,occurrence
, or both.
The 'rows'
option does not support cell arrays.
[[C](#bs%5F6vpd-1-C),[ia](#bs%5F6vpd-1-ia),[ic](#bs%5F6vpd-1-ic)] = unique(___)
also returns index vectors ia
and ic
using any of the previous syntaxes.
- If A is a vector, then
C = A(ia)
andA = C(ic)
. - If
A
is a matrix or array, thenC = A(ia)
andA(:) = C(ic)
. - If the
'rows'
option is specified, thenC = A(ia,:)
andA = C(ic,:)
. - If
A
is a table or a timetable, thenC = A(ia,:)
andA = C(ic,:)
.
[[C](#bs%5F6vpd-1-C),[ia](#bs%5F6vpd-1-ia),[ic](#bs%5F6vpd-1-ic)] = unique([A](#bs%5F6vpd-1-A),'legacy')
,[`C`,`ia`,`ic`] = unique(`A`,'rows','legacy')
,[`C`,`ia`,`ic`] = unique(`A`,[occurrence](#bs%5F6vpd-1-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 supportcategorical
arrays, datetime
arrays,duration
arrays, calendarDuration
arrays, tables, or timetables.
Examples
Define a vector with a repeated value.
Find the unique values of A
.
Create a table with some 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=5×3 table 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
. unique
returns the rows of A
in sorted order by the first variable Age
and then by the second variable Height
.
C=4×3 table Age Height Weight ___ ______ ______
Bob 38 64 131
Fred 38 71 176
George 40 67 185
Betty 43 69 163
Find the table rows with unique values in the first variable Age
. If you only want one table variable to contain unique values, you can use the indices returned by unique
to extract those rows from the table.
[C,ia] = unique(A.Age); B = A(ia,:)
B=3×3 table Age Height Weight ___ ______ ______
Fred 38 71 176
George 40 67 185
Betty 43 69 163
Define a vector with a repeated value.
Find the unique values of A
and the index vectors ia
and ic
, such that C = A(ia)
and A = C(ic)
.
Create a 10-by-3 matrix with some repeated rows.
A = 10×3
3 1 2
3 3 1
1 3 3
3 2 3
2 3 3
1 1 3
1 2 3
2 3 2
3 3 2
3 3 1
Find the unique rows of A
based on the data in the first two columns. Specify three outputs to return the index vectors ia
and ic
.
[C,ia,ic] = unique(A(:,1:2),'rows')
C = 7×2
1 1
1 2
1 3
2 3
3 1
3 2
3 3
ic = 10×1
5
7
3
6
4
1
2
4
7
7
Use ia
to index into A
and retrieve the rows that have unique combinations of elements in the first two columns.
uA = 7×3
1 1 3
1 2 3
1 3 3
2 3 3
3 1 2
3 2 3
3 3 1
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.
Find the unique elements in the vector. Return the index vectors ia
and ic
.
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.
a_counts = accumarray(ic,1); value_counts = [C, a_counts]
value_counts = 5×2
1 46
2 36
3 38
4 39
5 41
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')
Alternatively, you can specify 'sorted'
order.
[C, ia, ic] = unique(A,'sorted')
Define a vector containing missing values.
Find the unique values in the vector. unique
treats each instance of a missing value as a distinct value.
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.
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 = 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.
C = 6×1
3.1416
6.2832
9.4248
12.5664 15.7080 18.8496
Create a cell array of character vectors.
A = {'one','two','twenty-two','One','two'};
Find the unique character vectors contained in A
.
C = 1×4 cell {'One'} {'one'} {'twenty-two'} {'two'}
Create a string array, where some of the strings have trailing white space.
A = ["dog" "cat" "horse" "horse" "dog "];
Find the unique strings in the string array. unique
treats strings with trailing white space as distinct strings.
C = 1×4 string "cat" "dog" "dog " "horse"
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)
Find the unique elements of A
, and preserve the legacy behavior.
[C2, ia2, ic2] = unique(A, 'legacy')
Input Arguments
Input data, specified as an array, table, or timetable.
- If
A
is a table, thenunique
does not take row names into account. Two rows that have the same values but different names are considered equal. - If
A
is a timetable, thenunique
takes row times into account. Two rows that have the same values but different times are not considered equal. - If
A
is a categorical array, then the sort order is determined by the order of the categories. To see the sort order of a categorical array, use the categories function.
A
can also be an object with these class methods:
sort
(orsortrows
, if you specify the'rows'
option)ne
(not equal)
The methods must not have conflicting behaviors or outcomes.sort
or sortrows
must use a stable sorting algorithm. For example, you can specify A
as a heterogeneous array derived from a common root class, such as an array of graphics objects.
Order flag, specified as 'sorted'
or'stable'
, indicates the order of the values (or rows) in C.
Flag | Description |
---|---|
'sorted' | The values (or rows) in C return in sorted order as returned by sort.ExampleC = unique([5 5 3 4],'sorted')C = 3 4 5 |
'stable' | The values (or rows) in C return in the same order as inA.ExampleC = unique([5 5 3 4],'stable')C = 5 3 4 |
Data Types: char
| string
Occurrence flag, specified as 'first'
or'last'
, indicates whether ia should contain the first or last indices to repeated values found inA.
Occurrence Flag | Meaning |
---|---|
'first' | If there are repeated values (or rows) inA, 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. |
'last' | If there are repeated values (or rows) inA, 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. |
Data Types: char
| string
Output Arguments
Unique data of A, returned as an array. The class ofC
is the same as the class of the inputA
. The shape of C
depends on whether the input is a vector or a matrix:
- If the
'rows'
flag is not specified andA
is a row vector, thenC
is a row vector. - If the
'rows'
flag is not specified andA
is not a row vector, thenC
is a column vector. - If the
'rows'
flag is specified, thenC
is a matrix containing the unique rows ofA
.
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)
. IfA
is a table, or if the 'rows'
option is specified, then C = A(ia,:)
.
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, thenA = C(ic)
. - If
A
is a matrix or array, thenA(:) = C(ic)
. - If
A
is a table, or if the'rows'
option is specified, thenA = 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
This function supports tall arrays with the limitations:
- For tall vectors and tall tables, use the syntaxes:
C = unique(A)
[C,ia,ic] = unique(A)
- For tall matrices, use the syntaxes:
C = unique(A,'rows')
[C,ia,ic] = unique(A,'rows')
For more information, see Tall Arrays for Out-of-Memory Data.
Usage notes and limitations:
- When you do not specify the
'rows'
option:- The input
A
must be a vector. If you specify the'legacy'
option, the inputA
must be a row vector. - The first dimension of a variable-size row vector must have fixed length 1. The second dimension of a variable-size column vector must have fixed length 1.
- The input
[]
is not supported. Use a 1-by-0 or 0-by-1 input, for example,zeros(1,0)
, to represent the empty set. - If you specify the
'legacy'
option, then empty outputs are row vectors, 1-by-0. They are never 0-by-0.
- The input
- When you specify both the
'rows'
option and the'legacy'
option, outputsia
andic
are column vectors. If these outputs are empty, then they are 0-by-1, even if the outputC
is 0-by-0. - When the
setOrder
is not'stable'
or when you specify the'legacy'
option, the inputA
must already be sorted in ascending order. The first output,C
, is sorted in ascending order. - Complex inputs must be
single
ordouble
. - String input array is not supported.
The unique
function supports GPU array input with these usage notes and limitations:
- The
'legacy'
flag is not supported. - 64-bit integers are not supported.
- Specifying the
setOrder
andoccurrence
options together is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
You can specify the setOrder
and occurrence
arguments together. For example, specify [C,ia,ic] = unique(A,"stable","last")
. C
contains the unique elements, ordered as in the input data A
. ia
contains the indices of the last occurrence of these elements inA
. ic
is a mapping from elements inC
to elements in A
.
Previously, specifying both the setOrder
andoccurrence
arguments was not supported.