# MATLAB Unique

## Introduction

The unique function in MATLAB is a powerful tool for identifying and extracting unique elements from arrays, cell arrays, and tables. Its flexibility in handling different data types and providing additional information about indices makes it a valuable asset in various applications.

One such versatile function is unique, which plays a crucial role in identifying and extracting unique elements from arrays or tables, eliminating duplicates. In this comprehensive guide, we'll explore the syntax, applications, and nuances of the unique function in MATLAB.

### Syntax of the Unique Function

The unique function in MATLAB has a general syntax as follows:

A: Input array or vector.

C: Vector of unique values in the input array A.

a: Index vector that maps the values in C to the corresponding values in the input array A.

ic: Index vector that maps each element in the input array A to its corresponding value in C.

The output of the unique function provides not only the unique values in the Array but also information about their indices.

### Basic Usage

Let's start with a simple example to illustrate the basic usage of the unique function:

Output:

Explanation:

In this example, Array A contains duplicate values, and the unique function is applied to it. The unique values are stored in vector C, the indices of these values in the original Array are stored in vector ia, and the indices to reconstruct the original Array from unique values are stored in the vector ic.

Array Creation:

A = [2, 4, 1, 3, 2, 4, 3, 5];

An array named A is created with eight elements, some of which are duplicates.

Applying unique to the Array:

[C, ia, ic] = unique(A);

C: Contains the unique values of the original Array.

ia: Contains the indices of the unique values in the original Array.

ic: Contains the indices to reconstruct the original Array from unique values.

Displaying Unique Values:

disp('Unique Values:');

disp(C);

This displays the unique values found in the array A.

Displaying Indices of Unique Values in Original Array:

disp('Indices of Unique Values in Original Array:');

disp(ia);

This displays the indices of the unique values in the original Array. It shows the positions of the unique values within the original Array.

Displaying Indices to Reconstruct Original Array:

disp('Indices to Reconstruct Original Array:');

disp(ic);

This displays the indices that can be used to reconstruct the original Array from the unique values. It provides a way to map back from the unique values to the original Array.

Handling Different Data Types

One of the strengths of the unique function is its ability to handle various data types. It is not limited to numeric arrays; it can be applied to character arrays, cell arrays, and even tables.

Example with Character Array

Output:

In this example, a character array is used as input to the unique function. The same principles apply, and the output vectors C_char, ia_char, and ic_char provide information about the unique values and their indices.

Example with Cell Array

Output:

Here, a cell array containing strings is processed using the unique function. The resulting vectors provide insights into the unique values and their positions.

The unique function in MATLAB also offers advanced options to customize its behavior based on specific requirements.

Sorting the Output

By default, the unique function returns the unique values in the order they appear in the input array.

However, it can be useful to sort the output. This can be achieved using an additional argument:

Example:

Output:

In this example, the 'sorted' option is used to obtain the unique values in sorted order.

Stable Sorting

Sometimes, it is important to maintain the order of appearance for the first occurrence of each unique value.

The 'stable' option can be used for this purpose:

Output:

Here, the 'stable' option ensures that the order of the first occurrence of each unique value is maintained.

### Rows or Columns

Example:

The unique function can be applied to either rows or columns of a matrix by specifying the 'rows' or 'columns' option:

Output:

Explanation:

In this example, the unique function is applied to the rows of a matrix using the 'rows' option.

Matrix Creation:

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

A matrix named matrix is created with four rows and three columns. The first and third rows are identical.

Applying unique to Rows:

[C_rows, ia_rows, ic_rows] = unique(matrix, 'rows');

C_rows: Contains the unique rows of the original matrix.

ia_rows: Contains the indices of the unique rows in the original matrix.

ic_rows: Contains the indices to reconstruct the original matrix from unique rows.

Displaying Unique Rows:

disp('Unique Rows:');

disp(C_rows);

This displays the unique rows found in the matrix variable.

Displaying Indices of Unique Rows in Original Matrix:

disp('Indices of Unique Rows in Original Matrix:');

disp(ia_rows);

This displays the indices of the unique rows in the original matrix. In other words, it shows the positions of the unique rows within the original matrix.

Displaying Indices to Reconstruct Original Matrix:

disp('Indices to Reconstruct Original Matrix from Unique Rows:');

disp(ic_rows);

The code demonstrates how to identify and extract unique rows from a matrix using the unique function with the 'rows' option in MATLAB. The resulting unique rows, their indices in the original matrix, and indices to reconstruct the original matrix are then displayed.

Performance Considerations

When dealing with large datasets, the performance of the unique function becomes a crucial factor. Understanding the underlying mechanisms can help optimize its usage.

Time Complexity

The time complexity of the unique function depends on the algorithm used. MATLAB employs different algorithms based on the characteristics of the input data. For example, sorting algorithms are used when the data is numeric or character, and hashing is used for cell arrays.

Memory Usage

Memory consumption is another important consideration, especially when dealing with large datasets. Sorting algorithms may require additional memory, and this can impact the performance of the function.

Vectorization

Taking advantage of MATLAB's vectorized operations can significantly improve the performance of the unique function. Vectorized code is optimized for array operations, and MATLAB is designed to perform these operations efficiently.

Vectorized operations often outperform their loop-based counterparts in MATLAB.

Understanding the syntax and advanced options of the unique function allows users to tailor its behavior to specific requirements. Whether dealing with numeric arrays, character arrays, cell arrays, or tables, the unique function provides a consistent and efficient way to handle duplicate values in MATLAB.

• When working with large datasets, it's essential to consider the performance implications of the unique function.
• Optimizing code by taking advantage of vectorized operations and being aware of the underlying algorithms can lead to significant improvements in execution time and memory usage.

Next TopicMatlab Colon