unique
Unique values in array
Syntax
Description
returns the same data as inC
= unique(A
)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.
specifies which indices to return in case of repeated values.C
= unique(A
,occurrence
)occurrence
can be'first'
(default) or'last'
.
[
also returns index vectorsC
,ia
,ic
] = unique(___)ia
andic
using any of the previous syntaxes.
If
A
is a vector, thenC = 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
,ia
,ic
] = unique(A
,'legacy')[
,C
,ia
,ic
] = unique(A
,'rows','legacy')[
,andC
,ia
,ic
] = unique(A
,occurrence
,'legacy')[
preserve the behavior of theC
,ia
,ic
] = unique(A
,'rows',occurrence
,'legacy')unique
function from R2012b and prior releases.
The'legacy'
选项不支持分类数组dateti金宝appme arrays, duration arrays, calendarDuration arrays, tables, or timetables.
Examples
Unique Values in Vector
Define a vector with a repeated value.
A = [9 2 9 5];
Find the unique values ofA
.
C = unique(A)
C =1×32 5 9
Unique Rows in Table
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 tableAge 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 ofA
.unique
returns the rows ofA
in sorted order by the first variableAge
and then by the second variableHeight
.
C = unique(A)
C=4×3 tableAge 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 variableAge
. If you only want one table variable to contain unique values, you can use the indices returned byunique
to extract those rows from the table.
[C,ia] = unique(A.Age); B = A(ia,:)
B=3×3 tableAge Height Weight ___ ______ ______ Fred 38 71 176 George 40 67 185 Betty 43 69 163
Unique Values and Their Indices
Define a vector with a repeated value.
A = [9 2 9 5];
Find the unique values ofA
and the index vectorsia
andic
, such thatC = A(ia)
andA = C(ic)
.
[C, ia, ic] = unique(A)
C =1×32 5 9
ia =3×12 4 1
ic =4×13 1 3 2
Unique Rows in Matrix
Create a 10-by-3 matrix with some repeated rows.
A = randi(3,10,3)
A =10×33 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 ofA
based on the data in the first two columns. Specify three outputs to return the index vectorsia
andic
.
[C,ia,ic] = unique(A(:,1:2),'rows')
C =7×21 1 1 2 1 3 2 3 3 1 3 2 3 3
ia =7×16 7 3 5 1 4 2
ic =10×15 7 3 6 4 1 2 4 7 7
Useia
to index intoA
and retrieve the rows that have unique combinations of elements in the first two columns.
uA = A(ia,:)
uA =7×31 1 3 1 2 3 1 3 3 2 3 3 3 1 2 3 2 3 3 3 1
Count of Unique Elements
Find the unique elements in a vector and then useaccumarray
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 vectorsia
andic
.
[C,ia,ic] = unique(a);
Count the number of times each element inC
appears ina
. Specifyic
as the first input toaccumarray
and1
as the second input so that the function counts repeated subscripts inic
. Summarize the results.
a_counts = accumarray(ic,1); value_counts = [C, a_counts]
value_counts =5×21 46 2 36 3 38 4 39 5 41
Unique Values in Vector with Specified Order
Use thesetOrder
argument to specify the ordering of the values inC
.
Specify'stable'
if you want the values inC
to have the same order as inA
.
A = [9 2 9 5]; [C, ia, ic] = unique(A,'stable')
C =1×39 2 5
ia =3×11 2 4
ic =4×11 2 1 3
Alternatively, you can specify'sorted'
order.
[C, ia, ic] = unique(A,'sorted')
C =1×32 5 9
ia =3×12 4 1
ic =4×13 1 3 2
Unique Values in Array Containing NaNs
Define a vector containingNaN
.
A = [5 5 NaN NaN];
Find the unique values ofA
.
C = unique(A)
C =1×35 NaN NaN
unique
treatsNaN
values as distinct.
Unique Elements in Presence of Numerical Error
Create a vectorx
. Obtain a second vectory
by transforming and untransformingx
. This transformation introduces round-off differences iny
.
x = (1:6)'*pi; y = 10.^log10(x);
Verify thatx
andy
are not identical by taking the difference.
x-y
ans =6×110-14× 0.0444 0 0 0 0 -0.3553
Useunique
to find the unique elements in the concatenated vector[x;y]
. Theunique
function performs exact comparisons and determines that some values inx
are not exactly equal to values iny
. These are the same elements that have a nonzero difference inx-y
. Thus,c
contains values thatappearto be duplicates.
c = unique([x;y])
c =8×13.1416 3.1416 6.2832 9.4248 12.5664 15.7080 18.8496 18.8496
Useuniquetol
to perform the comparison using a small tolerance.uniquetol
treats elements that are within tolerance as equal.
C = uniquetol([x;y])
C =6×13.1416 6.2832 9.4248 12.5664 15.7080 18.8496
Unique Entries in Cell Array of Character Vectors
Create a cell array of character vectors.
A = {'one','two','twenty-two','One','two'};
Find the unique character vectors contained inA
.
C = unique(A)
C =1x4 cell array{'One'} {'one'} {'twenty-two'} {'two'}
Cell Array of Character Vectors with Trailing White Space
Create a cell array of character vectors,A
, where some of the vectors have trailing white space.
A = {'dog','cat','fish','horse','dog ','fish '};
Find the unique character vectors contained inA
.
C = unique(A)
C =1x6 cell array{'cat'} {'dog'} {'dog '} {'fish'} {'fish '} {'horse'}
unique
treats trailing white space in cell arrays of character vectors as distinct characters.
Preserve Legacy Behavior of unique
Use the'legacy'
flag to preserve the behavior ofunique
from R2012b and prior releases in your code.
Find the unique elements ofA
with the current behavior.
A = [9 2 9 5]; [C1, ia1, ic1] = unique(A)
C1 =1×32 5 9
ia1 =3×12 4 1
ic1 =4×13 1 3 2
Find the unique elements ofA
, and preserve the legacy behavior.
[C2, ia2, ic2] = unique(A,'legacy')
C2 =1×32 5 9
ia2 =1×32 4 3
ic2 =1×43 1 3 2
Input Arguments
A
—Input array
array
Input array.
If
A
is a table, thenunique
doesnottake 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.
A
can also be an object with the following class methods:
sort
(orsortrows
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. For example,A
can be an array of handles to graphics objects.
setOrder
—Order flag
'sorted'
(default) |'stable'
Order flag, specified as'sorted'
or'stable'
, indicates the order of the values (or rows) inC
.
Flag | Description |
---|---|
|
The values (or rows) in Example C = unique([5 5 3 4],'sorted')
C = 3 4 5 |
|
The values (or rows) in Example C = unique([5 5 3 4],'stable')
C = 5 3 4 |
Data Types:char
|string
occurrence
—Occurrence flag
'first'
(default) |'last'
Occurrence flag, specified as'first'
or'last'
, indicates whetheria
should contain the first or last indices to repeated values found inA
.
Occurrence Flag | Meaning |
---|---|
'last' |
If there are repeated values (or rows) inA , thenia contains the index to the last occurrence of the repeated value. For example:[C,ia,ic] = unique([9 9 9],'last','legacy') returnsia = 3 . This is the default behavior when the'legacy' flag is specified. |
'first' |
If there are repeated values (or rows) inA , thenia contains the index to the first occurrence of the repeated value. For example:[C,ia,ic] = unique([9 9 9],'first') returnsia = 1 . This is the default behavior. |
Data Types:char
|string
Output Arguments
C
— Unique data ofA
array
Unique data ofA
, returned as an array. The class ofC
is the same as the class of the inputA
. The shape ofC
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'
指定标记,thenC
is a matrix containing the unique rows ofA
.
ia
— Index toA
column vector
Index toA
, returned as a column vector of indices to thefirstoccurrence of repeated elements. When the'legacy'
指定标记,ia
is a row vector that contains indices to thelastoccurrence of repeated elements.
The indices generally satisfyC = A(ia)
. IfA
is a table, or if the'rows'
option is specified, thenC = A(ia,:)
.
ic
— Index toC
column vector
Index toC
, 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(:,
, wherevars
))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 usevartype
to create a subscript that selects variables of a specified type.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
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, seeTall Arrays for Out-of-Memory Data.
C/C++ Code Generation
生成使用M的C和c++代码ATLAB® Coder™.
Usage notes and limitations:
Code generation does not support cell arrays for the first argument.
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.
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
.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
The
'legacy'
flag is not supported.64-bit integers are not supported.
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Introduced before R2006a
Open Example
A modified version of this example exists on your system. Do you want to open this version instead?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)