How to use numel() In MATLAB

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


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()

...