How to use Setdiff() In MATLAB

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 =  5

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