How to detect duplicate values and its indices within an array in MATLAB? Last Updated : 11 Oct, 2021 Comments Improve Suggest changes 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 detect duplicate values and its indices within an array 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 Comparing Two Cell Arrays of Strings of Different Sizes in MATLAB MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Cell Array: A cell Array in MATLAB is a data type that can store any type of data. Data is stored in cells in a cell array. It is init 2 min read How to Iterate through each element in N-Dimensional matrix in MATLAB? In MATLAB, a matrix is considered a two-dimensional array of numbers. Other programming languages work with numbers but in MATLAB, every number is a matrix or array. To create a matrix in MATLAB, numbers are entered in each row by adding a comma or space and the ending of each row is marked by a sem 4 min read How to Create a Matrix From a Nested Loop in MATLAB? Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea 2 min read Cosine Similarity Calculation Between Two Matrices in MATLAB MATLAB (Matrix Laboratory) is a high-level programming language and numerical computing environment for performing complex mathematical computations and simulations. It is used in a wide range of applications including signal and image processing, control systems, and engineering and scientific calc 5 min read Like