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

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

  • Data Structure
  • Networking
  • RDBMS
  • Operating System
  • Java
  • MS Excel
  • iOS
  • HTML
  • CSS
  • Android
  • Python
  • C Programming
  • C++
  • C#
  • MongoDB
  • MySQL
  • Javascript
  • PHP
  • Physics
  • Chemistry
  • Biology
  • Mathematics
  • English
  • Economics
  • Psychology
  • Social Studies
  • Fashion Studies
  • Legal Studies
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

MATLABData AnalysisProgramming

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

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.

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 arraysA = [1, 2, 3, 2, 4, 3, 5, 6, 6, 7];% Array containing duplicate valuesB = [1, 2, 3, 4, 5, 6, 7];% Array containing unique values% Determine unique values in arraysUV1 = unique(A);UV2 = unique(B);% Display the resultsdisp('Array A:');disp(A);disp('Unique values in array A:');disp(UV1);% Display a messageif length(A) == length(UV1) fprintf('Each element is unique in array A.
');else fprintf('Array A has duplicate values.
');enddisp('Array B:');disp(B);disp('Unique values in array B:');disp(UV2);% Display a messageif 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 7Unique values in array A: 1 2 3 4 5 6 7Array A has duplicate values.Array B: 1 2 3 4 5 6 7Unique values in array B: 1 2 3 4 5 6 7Each 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 arrayA = [1, 2, 3, 2, 3, 4, 1, 5, 6, 7, 6];% Determine unique values in the arrayunique_values = unique(A);% Create an empty array to store duplicate valuesduplicate_values = [];% Iterate a loop through unique values to find duplicate valuesfor 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]; endend% Determine indices of duplicate values in the array% Create an empty array to store indices of duplicate valuesduplicate_ind = [];% Use a loop to find the indices of duplicate valuesfor i = 1:length(A) if ismember(A(i), duplicate_values) duplicate_ind = [duplicate_ind, i]; endend% Display duplicate values and their indicesfprintf('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 6Indices 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.

Manish Kumar Saini

Updated on: 10-Oct-2023

280 Views

Related Articles

  • How to detect duplicate values in primitive Java array?
  • How to Find Indices and Values of Nonzero Elements in MATLAB?
  • How to redundantly remove duplicate elements within an array – JavaScript?
  • Find Duplicate Elements and its Frequency in an Array in Java
  • How to find duplicate values in a JavaScript array?
  • How to remove duplicate property values in array – JavaScript?
  • Make an array of another array\'s duplicate values in JavaScript
  • How to Detect Face in Image Processing Using MATLAB?
  • PHP program to find standard deviation of values within an array
  • How to Use Logical Operator Within If Statements in MATLAB?
  • MongoDB aggregation group and remove duplicate array values?
  • How to remove rows in a Pandas series with duplicate indices?
  • MATLAB and Its Applications
  • How to check whether multiple values exist within a JavaScript array
  • Find Indices of Maximum and Minimum Value of Matrix in MATLAB
Kickstart Your Career

Get certified by completing the course

Get Started

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

Advertisem*nts

';adpushup.triggerAd(ad_id); });

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

References

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6472

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.