How to use Unique() In MATLAB

Unique(A) function is used to return the same data as in the specified array A without any repetitions.

Syntax: unique(A)

Example:

Matlab




% MATLAB code for detection of duplicate
% values of the array using unique()
% Initializing an array A
A = [1 2 3 4 5]
 
% Calling the unique() function over
% the above array A to return the
% unique values
B = unique(A)
 
% Using length() function to return
% the size of the above arrays
if length(A)==length(B)
    fprintf('Each elements are unique.')
else
    fprintf('Elements are repeated.')
end


Output:

A =
  1   2   3   4   5

B =
  1   2   3   4   5

Each elements are unique.

How to detect duplicate values and its indices within an array in MATLAB?

In this article, we will discuss how to find duplicate values and their indices within an array in MATLAB. It can be done using unique(), length(), setdiff(), and numel() functions that are illustrated below:

Similar Reads

Using Unique()

Unique(A) function is used to return the same data as in the specified array A without any repetitions....

Using Length()

...

Using Setdiff()

The length() function is used to return the length of the specified array....

Using numel()

...

Contact Us