Unique values in array - MATLAB unique (2024)

Unique values in array

collapse all in page

Syntax

C = unique(A)

C = unique(A,setOrder)

C = unique(A,occurrence)

C = unique(A,___,'rows')

C = unique(A,'rows',___)

[C,ia,ic] = unique(___)

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

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

[C,ia,ic]= unique(A,occurrence,'legacy')

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

Description

example

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) returns the unique values of A in a specific order. setOrder can be 'sorted' (default) or 'stable'.

C = unique(A,occurrence) specifies which indices to return in case of repeated values. occurrence can be 'first' (default) or 'last'.

example

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.

  • 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 the 'rows' option is specified, then C = A(ia,:) and A = C(ic,:).

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

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, datetime arrays, duration arrays, calendarDuration arrays, tables, or timetables.

Examples

collapse all

Unique Values in Vector

Open Live Script

Define a vector with a repeated value.

A = [9 2 9 5];

Find the unique values of A.

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

Unique Rows in Table

Open Live Script

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

Unique Values and Their Indices

Open Live Script

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

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

Unique Rows in Matrix

Open Live Script

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

Count of Unique Elements

Open Live Script

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.

a_counts = accumarray(ic,1);value_counts = [C, a_counts]
value_counts = 5×2 1 46 2 36 3 38 4 39 5 41

Unique Values in Vector with Specified Order

Open Live Script

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 = 1×3 9 2 5
ia = 3×1 1 2 4
ic = 4×1 1 2 1 3

Alternatively, you can specify 'sorted' order.

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

Unique Values in Array Containing NaNs

Open Live Script

Define a vector containing NaN.

A = [5 5 NaN NaN];

Find the unique values of A.

C = unique(A)
C = 1×3 5 NaN NaN

unique treats NaN values as distinct.

Unique Elements in Presence of Numerical Error

Open Live Script

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×110-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.

C = uniquetol([x;y])
C = 6×1 3.1416 6.2832 9.4248 12.5664 15.7080 18.8496

Unique Entries in Cell Array of Character Vectors

Open Live Script

Create a cell array of character vectors.

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

Find the unique character vectors contained in A.

C = unique(A)
C = 1x4 cell {'One'} {'one'} {'twenty-two'} {'two'}

Cell Array of Character Vectors with Trailing White Space

Open Live Script

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 in A.

C = unique(A)
C = 1x6 cell {'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

Open Live Script

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 = 1×3 2 5 9
ia1 = 3×1 2 4 1
ic1 = 4×1 3 1 3 2

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

[C2, ia2, ic2] = unique(A, 'legacy')
C2 = 1×3 2 5 9
ia2 = 1×3 2 4 3
ic2 = 1×4 3 1 3 2

Input Arguments

collapse all

AInput array
array

Input array.

  • 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 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. For example, A can be an array of handles to graphics objects.

setOrderOrder flag
'sorted' (default) | 'stable'

Order flag, specified as 'sorted' or 'stable', indicates the order of the values (or rows) in C.

FlagDescription

'sorted'

The values (or rows) in C return in sorted order as returned by sort.

Example

C = unique([5 5 3 4],'sorted')
C = 3 4 5

'stable'

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

Example

C = unique([5 5 3 4],'stable')
C = 5 3 4

Data Types: char | string

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

Data Types: char | string

Output Arguments

collapse all

C — Unique data of A
array

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 A
column 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 C
column 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

Version History

Introduced before R2006a

See Also

union | intersect | ismember | issorted | setdiff | setxor | sort | uniquetol

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.

Unique values in array - MATLAB unique (1)

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:

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
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Unique values in array - MATLAB unique (2024)

FAQs

How do you find unique values in an array in Matlab? ›

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.

How do you store unique values in an array? ›

To do so, simply create a new variable and set it to an array object. Next, use the spread operator and the set constructor with your duplicate array as the value. The result will be a new array with only the unique values from the original array.

How does accumarray work? ›

accumarray accumulates the elements of the data vector by group, and then applies the function fun to the group elements. When you specify fun = [] , the computation uses the default function sum . The specified function must accept a column vector and return a numeric, logical , or char scalar, or a scalar cell .

How do you find unique values in an array list? ›

Algorithm
  1. Create and populate an ArrayList with elements.
  2. Create a HashSet object.
  3. Iterate through each element in an ArrayList.
  4. Add each element to the HashSet using add() method.
  5. Once traversing an ArrayList, HashSet will contain only those unique values from that original list.
Jul 25, 2023

How do you check if an array has unique values? ›

Using `Array. filter()` with a callback that checks if an element's index is equal to its first occurrence's index in the array (`Array. indexOf()`), the function filters out duplicate elements. The resulting array's length equals the original if it contains only unique values.

How do you get unique values in an object array? ›

How to get distinct values from an array of objects in JavaScript...
  1. Using map() and filter() Methods.
  2. Use Set() Constructor.
  3. Using map(), set() and from() Methods.
  4. Using Lodash _.uniqBy() method and map()
  5. Using reduce() Method.
Jun 10, 2024

What are unique elements in an array? ›

You are given an integer array nums . The unique elements of an array are the elements that appear exactly once in the array.

How do you store random values in an array? ›

We can assign random values to an array by two approaches. If we do not pass any arguments to the nextInt() method, then it will assign some arbitrary random value of integers range in Java and if we pass some argument to the nextInt() method then it will generate numbers in the range between 0 to the argument passed.

How to use c*msum in Matlab? ›

B = c*msum( A , dim ) returns the cumulative sum of the elements along dimension dim . For example, if A is a matrix, then c*msum(A,2) returns the cumulative sum along the rows of A . B = c*msum(___, direction ) specifies the direction for any of the previous syntaxes.

How to find the number of elements in an array in Matlab? ›

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .

How to use splitapply in Matlab? ›

The splitapply function treats the variables of T as vectors, matrices, or cell arrays, depending on the data types and sizes of the table variables. If T has N variables, then func must accept N input arguments. [Y1,...,YM] = splitapply(___) splits variables into groups and applies func to each group.

How do you find unique numbers in an array? ›

In order to get the distinct element from the array, we will first sort the array in ascending or descending order so that each element's occurrence becomes consecutive. After that, we will traverse that sorted array using the loop and skip all the consecutive repeated elements' index.

How do you find unique pairs in an array? ›

Find Unique pair in an array with pairs of numbers
  1. XOR each element of the array and you will left with the XOR of two different elements which are going to be our result. ...
  2. Now find a set bit in XOR.
  3. Now divide array elements in two groups. ...
  4. XOR of elements present in first group would be our first element.
Jul 19, 2022

How do you check if an array has duplicate values in Matlab? ›

How to detect duplicate values and its indices within an array in...
  1. Syntax: unique(A)
  2. Syntax: length(X)
  3. Syntax: setdiff(A, B)
  4. Syntax: numel(A)
Oct 11, 2021

References

Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6476

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.