Main Content

unique

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.

  • If A is a categorical array, then the sort order is determined by the order of the categories.

example

C = unique(A,setOrder) specifies whether to return the unique values of A in sorted or stable order.

example

C = unique(___,occurrence) specifies whether to return the first or last indices of repeated values for either of the previous syntaxes.

C = unique(___,"rows") treats each row of A as a single entity and returns the unique rows of A in sorted order. The "rows" option is not supported when the input data is a cell array of character vectors.

example

C = unique(___,"legacy") preserves the behavior of the unique function from R2012b and previous releases. Specifying the "legacy" option with TreatMissingAsDistinct or with both the setOrder and occurrence arguments is not supported. The "legacy" option does not support categorical arrays, datetime arrays, duration arrays, calendarDuration arrays, tables, or timetables.

C = unique(___,TreatMissingAsDistinct=tf) specifies whether to treat missing values as distinct. (since R2026a)

example

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

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

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

  • If you specify the "rows" option, then C = A(ia,:) and A = C(ic,:).

  • If A is a table or timetable, then C = A(ia,:) and A = C(ic,:).

example

Examples

collapse all

Create a vector with a repeated value. Then, find the unique values of the vector.

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

     2     5     9

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 = unique(A)
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 want only 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  

Create a vector with a repeated value. Find the unique values of A, and return the index vectors ia and ic, such that C = A(ia) and A = C(ic).

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

     2     5     9

ia = 3×1

     2
     4
     1

ic = 4×1

     3
     1
     3
     2

Create a 10-by-3 matrix with some repeated rows.

A = randi(3,10,3)
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

ia = 7×1

     6
     7
     3
     5
     1
     4
     2

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 = A(ia,:)
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, and return the index vectors ia and ic.

a = randi([1 5],200,1);
[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.

a_counts = accumarray(ic,1);
value_counts = [C, a_counts]
value_counts = 5×2

     1    46
     2    36
     3    38
     4    39
     5    41

Create a vector with a repeated value. Find the unique values in the vector, and return them in the same order as they appear in the vector by specifying the set order as "stable".

A = [9 2 9 5];
C = unique(A,"stable")
C = 1×3

     9     2     5

Create a vector containing missing values. Find the unique values in the vector. unique treats each instance of a missing value as a distinct value.

A = [5 8 NaN NaN];
C = unique(A)
C = 1×4

     5     8   NaN   NaN

Alternatively, treat each instance of a missing value as a duplicate value (since R2026a).

C2 = unique(A,TreatMissingAsDistinct=false)
C2 = 1×3

     5     8   NaN

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

Concatenate the vectors x and y. Then, use unique to find the unique elements. 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.

A = [x; y];
C = unique(A)
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.

Ctol = uniquetol(A)
Ctol = 6×1

    3.1416
    6.2832
    9.4248
   12.5664
   15.7080
   18.8496

Create a string array. Find the unique character vectors contained in A.

A = ["one" "two" "twenty-two" "One" "two"];
C = unique(A)
C = 1×4 string
    "One"    "one"    "twenty-two"    "two"

Create a string array, where one of the strings has trailing white space. Find the unique strings in the array. unique treats strings with trailing white space as distinct strings.

A = ["dog" "cat" "horse" "horse" "dog "];
C = unique(A)
C = 1×4 string
    "cat"    "dog"    "dog "    "horse"

Input Arguments

collapse all

Input data, specified as an array, table, or timetable.

  • If A is a table, then unique 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, then unique 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 (or sortrows, 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 of returned values in C, specified as one of the values in this table.

ValueDescription

"sorted"

Return the values (or rows) in C in sorted order as returned by sort.

"stable"

Return the values (or rows) in C in the same order as in A.

Example: C = unique([5 5 3 4],"sorted") returns the values in sorted order as C = [3 4 5].

Example: C = unique([5 5 3 4],"stable") returns the values in stable order as C = [5 3 4].

Occurrence for returned indices in ia, specified as one of the values in this table.

ValueDescription
"first"If A has repeated values (or rows), then ia contains the index to the first occurrence of the repeated value.
"last"If A has repeated values (or rows), then ia contains the index to the last occurrence of the repeated value.

Example: [C,ia,ic] = unique([9 9 9],"first") returns the index to the first occurrence of the repeated value as ia = 1.

Example: [C,ia,ic] = unique([9 9 9],"last") returns the index to the last occurrence of the repeated value as ia = 3.

Since R2026a

Option to treat missing values as distinct, specified as one of these values:

  • true or 1 — Treat missing values as distinct. Each instance of a missing value in the input data is included in C.

  • false or 0 — Treat each repeated instance of a missing value as a duplicate. At most one missing value is included in C. If the input data is a table, or if you specify the "rows" option, then C might contain multiple rows with missing values. Rows are duplicates if they have missing values in the same columns and matching nonmissing values in the other columns.

Example: C = unique(A,TreatMissingAsDistinct=false) treats each instance of a missing value as a duplicate.

Data Types: logical

Output Arguments

collapse all

Unique data of A, returned as an array, table, or timetable. The class of C is the same as the class of the input A. The shape of C depends on the shape of A.

  • If you do not specify the "rows" option and A is a row vector, then C is a row vector.

  • If you do not specify the "rows" option and A is a column vector, then C is a column vector.

  • If you do not specify the "rows" option, then C is a matrix containing the unique rows of A.

  • If A is a table or timetable, then C is a table or timetable with the same number of variables as A.

Index to A, returned as a column vector of indices to the first occurrence of repeated elements. If you specify occurrence as "last", then ia contains indices to the last occurrence of repeated elements.

The indices generally satisfy C = A(ia). If A is a table or timetable, or if you specify the "rows" option, then C = A(ia,:).

Index to C, returned as a column vector.

  • 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 timetable, or if you specify the "rows" option, 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 variable 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

expand all

Version History

Introduced before R2006a

expand all