How to detect duplicate values and its indices within an array in MATLAB? Last Updated : 11 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Using Unique() 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.Using Length() The length() function is used to return the length of the specified array. Syntax: length(X) Example: Matlab % MATLAB code for detection of duplicate % values of the array using length() % Initializing an array A A = [1 2 3 2 4 3 5 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 2 4 3 5 5 B = 1 2 3 4 5 Elements are repeated.Using Setdiff() The setdiff() function is used to return the set difference between the two given arrays i.e. the data present in array A but not in B, without any data repetitions. Syntax: setdiff(A, B) Example: Matlab % MATLAB code for detection of duplicate % values of the array using setdiff() % Initializing an array A = [1 2 3 4 3] % Calling the unique() function % over the above array to return % unique elements [B] = unique(A) % Using setdiff() and numel() functions % together to get the indices of repeated % elements duplicate_indices = setdiff(1:numel(A), B) Output: A = 1 2 3 4 3 B = 1 2 3 4 duplicate_indices = 5Using numel() The numel() function is used to return the number of elements present in a specified array. Syntax: numel(A) Example: Matlab % MATLAB code for detection of duplicate % values of the array using numel() % Initializing an array A = [0 2 4 1 2 3 0 4] % Calling the unique() function % over the above array to return % unique elements [B] = unique(A) % Using setdiff() and numel() functions % together to get the indices of repeated % elements duplicate_indices = setdiff(1:numel(A), B) Output: A = 0 2 4 1 2 3 0 4 B = 0 1 2 3 4 duplicate_indices = 5 6 7 8 Comment More infoAdvertise with us Next Article How to Find Indices and Values of Nonzero Elements in MATLAB? K Kanchan_Ray Follow Improve Article Tags : Software Engineering MATLAB-programs MATLAB Array-Programs Similar Reads How to Find Indices and Values of Nonzero Elements in MATLAB? MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector. Syntax: vec = [] %so 3 min read How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi 4 min read How to Find the Position of a Number in an Array in MATLAB? Finding the position of a number in an array, which can be done using the find() function. The find() function is used to find the indices and values of the specified nonzero elements. Syntaxfind(X) Parameters: This function accepts a parameter. X: This is the specified number whose position is goin 1 min read How to find sum of elements of an array in MATLAB? This article will discuss the "Finding sum of elements of an array" in MATLAB that can be done using multiple approaches which are illustrated below. Â Using sum(A)Â This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a ro 4 min read How to extract numbers from cell array in MATLAB? In this article, we are going to discuss the extraction of numbers from the cell array with the help of regexp(), str2double(), cat(), and isletter() functions. What is a Cell Array? A cell array is nothing but a data type having indexed data containers called cells, where each cell contains any typ 3 min read MATLAB Find Exact String in Cell Array Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions, 2 min read Like