Matlab

How to Use the unique Function in MATLAB

The following article explains how to use the unique() function in MATLABĀ®. This function sorts the elements of an array and removes duplicate values.

Its main properties and the different modes of use are also described in detail in this document.

This tutorial includes practical examples as well as some of the most common errors, the corresponding error messages, and how to fix them to make programming easier.

Matlab Unique Syntax

This is the basic syntax of this function:

C = unique(a)

There are several ways to sort the data and how it is treated by the unique() function. The syntax for each of those cases is shown below.

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

C =  Unique data of A.
ia = Column vector Index of A
ic = Column vector Index of C.

Description and Examples of Unique

The unique function returns in “C” a list with the data sent in “A”, ordered and without repetitions. This function also has outputs “ia” and “ic”, which return the index of the data sent in “A” and returned in “C”. The sorting mode and the treatment of the data can be selected using flags sent in the function call.

In the following examples, we will see the different ways of using this function, its variants in sorting and handling the input data.

How to Order the Elements of an Array and Eliminate Its Repeated Elements with the Unique Function of MATLAB

In the following example, we see the basic way of using this function where we send as input argument a numeric array “A” with 4×4 elements, unordered and with repetitions.

A = [8, 1, 2, 8; 9, 10, 9, 5;
     8, 1, 2, 8; 9, 10, 9, 5];
C =unique(A)

C = 1   2   5   8   9   10

As a result, the unique function returns a list with the values ordered from least to greatest and without repetition.

How to Order the Elements of an Array, Eliminate Its Repeated Elements and Obtain the Index of the Input and Output Data

In this example, we will see how to get the index of “A” (ia) and “C” (ic) as well as the order of the data of “A” and the elimination of repetitions.

A = [1, 2, 3, 4, 5 ; 10, 11, 12, 13, 14; 5, 12, 1, 13, 3];
[C, ia, ic] = unique(A)

As a result, the unique() function will return the results of the ordering of “A” in “C”.

C = 1  2  3  4  5  10  11  12  13  14

The index of A:

 ia =1  4  7  10  3  2  5  6  11  14

And the index of C:

 ic =1  6  5  2  7  8  3  8  1  4  9  9  5  10  3

The option “occurrence” specifies by the flags “first” and “last” which index should be returned if the values are repeated.

How to Select the Data Ordering Mode Using the Unique Function in MATLAB

Now, let us look at the “setOrder” option of the unique function which allows us to specify the ordering mode of the data returned in “C” by using the “sorted” and “stable” flags.

If the unique() function is called without the “setOrder” input, it will be sent in “ordered” mode by default and the elements in “C” will be returned in order from smallest to largest. If unique is called with the “setOrder” input in “stable” mode, the elements in “C” are returned in the same input order and without repetitions.

The syntax is as follows:

C = unique(A,setOrder)

Example:

A = [8, 2, 8 ; 9, 10, 9]
C = unique (A, "sorted")

As seen in the following figure, when unique() is called with the “sorted” flag, the elements in “C” are returned in order from smallest to largest without repetition.

C = 2   8   9  10

The flag “stable” returns the data in “C” in the same input order, just without repetitions.

A = [8, 2, 8 ; 9, 10, 9]
C = unique(A,"stable")

C =  8   9   2   10

How to Do the Ordering of Elements in a Cell Array of Character Vectors Using the Unique Function

In this example, we will see the sorted of elements in a cell array of character vectors using a unique() function. To do this, we will create a cell array of character vectors “A” with unordered and repeated elements.

A = {'a','d','c','b','a','d','e','d','e'};
C=unique(A)

As a result, unique() returns in “C” a cell array of character vectors with all its elements sorted.

C =    {'a'}    {'b'}    {'c'}    {'d'}    {'e'}

How to Order the Rows Contained in an Array with the Unique Function

In this example, the unique() function returns the rows contained in an array sorted and with the occurrences removed. For this example, we create an array with 6×5 elements “A” and call the unique() function. In this case, the function takes each row as a unique element and returns the rows in order from smallest to largest, removing duplicates.

A = [51,  2,  3,  2, 5 ;
     10, 11, 12, 11, 14;
     10, 11, 12, 11, 14;
     30, 31,  3,  2, 34;
     45,  3, 21, 43, 43;
     35,  3, 41, 48, 43];

C= unique(A, 'rows'

The unique() function in this case will return in “C”.

C =
    10    11    12   11   14
    30    31     3    2   34
    35     3    41   48   43
    45     3    21   43   43
    51     2     3    2    5

How to Select the Rows of an Array That You Want to Order with the Unique Function

In this example, the unique() function will return the rows contained in an array, sort and remove duplicates. For this example, we will create a 6×5 array “A” of elements and call the function. In this case, it will take each row as a single element and return the rows in order from smallest to largest, eliminating duplicates.

A = [51,  2,  3,  2,  5;
     10, 11, 12, 11, 14;
     10, 11, 12, 11, 14;
     30, 31,  3,  2, 34;
     45,  3, 21, 43, 43;
     35,  3, 41, 48, 43];

C = unique (A (:, 3: 5), 'rows')

The unique() function in this case will return in “C”.

C =

     3     2     5
     3     2    34
    12    11    14
    21    43    43
    41    48    43

Conclusion

In this article, I have explained how to use the MATLAB unique() function to order the elements of an array, and I have included some practical examples that show how to use the function in all its variants. Also, all supported input arguments and data types have been described in detail. We hope that you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).