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


In this tutorial, I will explain how to detect duplicate values and its indices within an array in MATLAB. To accomplish this task, we can use various built-in functions in MATLAB. In this article, I will do this using the following two function combinations.

  • unique() and length()

  • unique() and ismember()

Before going to implement MATLAB codes, let us first know the steps involved to detect duplicate values in an array.

How to Detect Duplicate Values and their Indices in an Array using MATLAB?

In MATLAB, the following steps are involved in detecting duplicate values and their indices in an array:

Step (1) − Input or create an array.

Step (2) − Use a function to return the unique values in the array.

Step (3) − Display the results.

Let us consider examples to practically understand how to implement these three steps to detect duplicate values in an array using MATLAB programming.

(1). Detect Unique Values in Array using "unique() & length()" Functions

In MATLAB, there are two built-in functions "unique" and "length()" that can be used to detect duplicate values in an array.

Syntax

unique_values = unique(input_array);

Example

Consider the following MATLAB program to understand how to use the "unique" and "length" functions to detect duplicate values in an array.

% MATLAB code to detect duplicate values in an array
% Create the sample arrays
A = [1, 2, 3, 2, 4, 3, 5, 6, 6, 7];	% Array containing duplicate values
B = [1, 2, 3, 4, 5, 6, 7];	% Array containing unique values

% Determine unique values in arrays
UV1 = unique(A);
UV2 = unique(B);

% Display the results
disp('Array A:');
disp(A);
disp('Unique values in array A:');
disp(UV1);

% Display a message
if length(A) == length(UV1)
   fprintf('Each element is unique in array A.
'); else fprintf('Array A has duplicate values.
'); end disp('Array B:'); disp(B); disp('Unique values in array B:'); disp(UV2); % Display a message if length(B) == length(UV2) fprintf('Each element is unique in array B.
'); else fprintf('Array B has duplicate values.
'); end

Output

Array A:
     1     2     3     2     4     3     5     6     6     7

Unique values in array A:
     1     2     3     4     5     6     7

Array A has duplicate values.
Array B:
     1     2     3     4     5     6     7

Unique values in array B:
     1     2     3     4     5     6     7

Each element is unique in array B.

(2). Detect Unique Values and their Indices in Array using "unique() & ismember()" Functions

In MATLAB, we can use the "unique" function to detect unique values in an array and the "ismember" function to detect the indices of duplicate values in the array.

Example

Let us take an example to understand how to use the "unique" and "ismember" functions to detect duplicate values and their indices in the array.

% MATLAB code to find duplicate values and their indices in an array
% Create a sample array
A = [1, 2, 3, 2, 3, 4, 1, 5, 6, 7, 6];

% Determine unique values in the array
unique_values = unique(A);

% Create an empty array to store duplicate values
duplicate_values = [];

% Iterate a loop through unique values to find duplicate values
for i = 1:length(unique_values)
   value = unique_values(i);
   indices = find(A == value);
    
   % If there are more than one index then it is duplicate
   if numel(indices) > 1
      duplicate_values = [duplicate_values, value];
   end
end

% Determine indices of duplicate values in the array
% Create an empty array to store indices of duplicate values
duplicate_ind = [];

% Use a loop to find the indices of duplicate values
for i = 1:length(A)
   if ismember(A(i), duplicate_values)
      duplicate_ind = [duplicate_ind, i];
   end
end

% Display duplicate values and their indices
fprintf('Duplicate values in the array: %s
', num2str(duplicate_values)); fprintf('Indices of duplicate values: %s
', num2str(duplicate_ind));

Output

Duplicate values in the array: 1  2  3  6
Indices of duplicate values: 1   2   3   4   5   7   9  11

Code Explanation

In this MATLAB code, we start by defining an example array containing multiple duplicate values. Then, we use the "unique" function to find the unique values in the array.

Next, we create an empty array to store the duplicate values. To find the duplicate values, we loop through the unique values and if found any duplicate value it is stored in the array "duplicate_values". In this loop, we check if there is more than one index for a value in the array, then it is duplicate and stored in the duplicate array.

In the next segment of the code, we detect the indices of duplicate values. For this, we create an empty array "duplicate_ind" that will store the indices of duplicate value in the array. To detect the duplicate indices, we use the "ismember" function.

Finally, we display the duplicate values and their indices as output.

Conclusion

This is all about detecting duplicate values and their indices in an array using MATLAB programming. MATLAB provides various methods to accomplish this task. In this tutorial, I have explained how we can detect duplicate values and their indices in an array using combination of MATLAB’s built−in functions i.e., "unique & length" and "unique & ismember". You can try these MATLAB codes with your arrays as well.

Updated on: 10-Oct-2023

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements