如何在 MATLAB 中检测数组中的重复值及其索引?
在本文中,我们将讨论如何在 MATLAB 中查找数组中的重复值及其索引。可以使用unique() 、 length() 、 setdiff()和numel()函数来完成,如下所示:
使用 Unique()
Unique(A)函数用于返回与指定数组 A 中相同的数据,没有任何重复。
Syntax: unique(A)
例子:
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
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
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)
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)
输出:
A =
1 2 3 4 5
B =
1 2 3 4 5
Each elements are unique.
使用 Length()
length()函数用于返回指定数组的长度。
Syntax: length(X)
例子:
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
输出:
A =
1 2 3 2 4 3 5 5
B =
1 2 3 4 5
Elements are repeated.
使用 Setdiff()
setdiff()函数用于返回两个给定数组之间的集合差,即数组 A 中存在但不在 B 中的数据,没有任何数据重复。
Syntax: setdiff(A, B)
例子:
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)
输出:
A =
1 2 3 4 3
B =
1 2 3 4
duplicate_indices = 5
使用 numel()
numel()函数用于返回指定数组中存在的元素数。
Syntax: numel(A)
例子:
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)
输出:
A =
0 2 4 1 2 3 0 4
B =
0 1 2 3 4
duplicate_indices =
5 6 7 8