Matlab

How to Use the unique Function in MATLAB

In MATLAB, data analysis often involves working with datasets containing duplicate elements. With the help of the unique function, which is a potent tool for locating and extracting unique values from an array or matrix, you can gain important insights into data trends. In this article, we will explore how to use the unique function in MATLAB effectively, discussing its syntax and presenting practical examples to illustrate its utility.

unique Function in MATLAB

The unique function in MATLAB is used to identify unique elements in a dataset, its syntax is as follows:

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

Here, A represents the input array or matrix, C stores the unique values, ia stores the indices of the first occurrences of the unique values, and ic represents the indices that map the original array to the unique values.

Example 1: Finding Unique Values in a Numeric Array

% Define a numeric array with duplicate elements

data = [3, 2, 5, 2, 1, 3, 5];

% Find unique values

uniqueValues = unique(data);

% Display the unique values

disp(uniqueValues);

In this example, we have a numeric array data containing duplicate elements. By applying the unique function, we obtain the unique values from the array, which are then stored in the uniqueValues variable.

Example 2: Extracting Unique Rows from a Matrix

% Create a matrix with duplicate rows

matrix = [1, 2, 3; 4, 5, 6; 1, 2, 3; 7, 8, 9];

% Find unique rows

[uniqueRows, ~, ~] = unique(matrix, 'rows', 'stable');

% Display the unique rows

disp(uniqueRows);

In this example, we have a matrix containing duplicate rows. By specifying the rows option, the unique function considers each row as an individual entity. The resulting unique rows are stored in the uniqueRows variable using the unique function. The stable option ensures that the order of the unique rows is preserved.

A screenshot of a computer Description automatically generated with low confidence

Example 3: Extracting Unique Rows from a Matrix with Preserved Order

% Create a matrix with duplicate rows

matrix = [1, 2, 3; 4, 5, 6; 1, 2, 3; 7, 8, 9];

% Find unique rows preserving the order

[C, ia, ic] = unique(matrix, 'rows', 'stable');

% Display the unique rows, their indices, and original mapping

disp("Unique Rows:");

disp(C);

disp("Indices of First Occurrences:");

disp(ia);

disp("Indices Mapping Original to Unique Rows:");

disp(ic);

In this example, we have a matrix with duplicate rows. By using the unique function with the rows option, each row is considered an individual entity. The stable option ensures that the order of the unique rows is preserved.

After applying the unique function, we obtain three outputs: C represents the unique rows, ia contains the indices of the first occurrences of the unique rows, and ic stores the indices that map the original matrix to the unique rows.

The example then proceeds to display the unique rows, their indices of first occurrences, and the indices mapping the original matrix to the unique rows.

By utilizing the [C, ia, ic] = unique(A, ‘rows’, ‘stable’) syntax, you gain a comprehensive understanding of the unique rows within a matrix, their corresponding indices, and the mapping from the original matrix to the unique rows.

A screenshot of a computer Description automatically generated

Conclusion

The unique function in MATLAB is a powerful tool for extracting unique values and rows from arrays and matrices. Whether you are handling numeric arrays or complex matrices, the unique function equips you with the necessary functionality to uncover essential patterns and insights in your data.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.