如何在 MATLAB 中检测数组中的重复值及其索引?
在本教程中,我将解释如何在 MATLAB 中检测重复值及其在数组中的索引。为了完成此任务,我们可以使用 MATLAB 中的各种内置函数。在本文中,我将使用以下两种函数组合来实现。
unique() 和 length()
unique() 和 ismember()
在开始实现 MATLAB 代码之前,让我们首先了解在数组中检测重复值所涉及的步骤。
如何使用 MATLAB 检测数组中的重复值及其索引?
在 MATLAB 中,检测数组中重复值及其索引涉及以下步骤
步骤 (1) - 输入或创建数组。
步骤 (2) - 使用函数返回数组中的唯一值。
步骤 (3) - 显示结果。
让我们考虑一些示例,以便从实践上了解如何实现这三个步骤,以使用 MATLAB 编程在数组中检测重复值。
(1). 使用“unique() & length()”函数检测数组中的唯一值
在 MATLAB 中,有两个内置函数“unique”和“length()”可用于检测数组中的重复值。
语法
unique_values = unique(input_array);
示例
请考虑以下 MATLAB 程序,以了解如何使用“unique”和“length”函数在数组中检测重复值。
% MATLAB code to detect duplicate values in an array
% Create the sample arrays
A = [1, 2, 3, 2, 4, 3, 5, 6, 6, 7]; % Array containing duplicate values
B = [1, 2, 3, 4, 5, 6, 7]; % Array containing unique values
% Determine unique values in arrays
UV1 = unique(A);
UV2 = unique(B);
% Display the results
disp('Array A:');
disp(A);
disp('Unique values in array A:');
disp(UV1);
% Display a message
if length(A) == length(UV1)
fprintf('Each element is unique in array A.
');
else
fprintf('Array A has duplicate values.
');
end
disp('Array B:');
disp(B);
disp('Unique values in array B:');
disp(UV2);
% Display a message
if length(B) == length(UV2)
fprintf('Each element is unique in array B.
');
else
fprintf('Array B has duplicate values.
');
end
输出
Array A:
1 2 3 2 4 3 5 6 6 7
Unique values in array A:
1 2 3 4 5 6 7
Array A has duplicate values.
Array B:
1 2 3 4 5 6 7
Unique values in array B:
1 2 3 4 5 6 7
Each element is unique in array B.
(2). 使用“unique() & ismember()”函数检测数组中的唯一值及其索引
在 MATLAB 中,我们可以使用“unique”函数检测数组中的唯一值,并使用“ismember”函数检测数组中重复值的索引。
示例
让我们举一个例子来了解如何使用“unique”和“ismember”函数检测数组中重复值及其索引。
% MATLAB code to find duplicate values and their indices in an array
% Create a sample array
A = [1, 2, 3, 2, 3, 4, 1, 5, 6, 7, 6];
% Determine unique values in the array
unique_values = unique(A);
% Create an empty array to store duplicate values
duplicate_values = [];
% Iterate a loop through unique values to find duplicate values
for i = 1:length(unique_values)
value = unique_values(i);
indices = find(A == value);
% If there are more than one index then it is duplicate
if numel(indices) > 1
duplicate_values = [duplicate_values, value];
end
end
% Determine indices of duplicate values in the array
% Create an empty array to store indices of duplicate values
duplicate_ind = [];
% Use a loop to find the indices of duplicate values
for i = 1:length(A)
if ismember(A(i), duplicate_values)
duplicate_ind = [duplicate_ind, i];
end
end
% Display duplicate values and their indices
fprintf('Duplicate values in the array: %s
', num2str(duplicate_values));
fprintf('Indices of duplicate values: %s
', num2str(duplicate_ind));
输出
Duplicate values in the array: 1 2 3 6 Indices of duplicate values: 1 2 3 4 5 7 9 11
代码解释
在此 MATLAB 代码中,我们首先定义一个包含多个重复值的示例数组。然后,我们使用“unique”函数查找数组中的唯一值。
接下来,我们创建一个空数组来存储重复值。为了查找重复值,我们遍历唯一值,如果找到任何重复值,则将其存储在数组“duplicate_values”中。在此循环中,我们检查数组中某个值的索引是否超过一个,如果是,则它是重复的,并存储在重复数组中。
在代码的下一部分,我们检测重复值的索引。为此,我们创建一个空数组“duplicate_ind”,它将存储数组中重复值的索引。为了检测重复索引,我们使用“ismember”函数。
最后,我们将重复值及其索引显示为输出。
结论
这就是使用 MATLAB 编程检测数组中重复值及其索引的全部内容。MATLAB 提供了多种方法来完成此任务。在本教程中,我解释了如何使用 MATLAB 内置函数的组合(即“unique & length”和“unique & ismember”)来检测数组中重复值及其索引。您也可以在自己的数组中尝试这些 MATLAB 代码。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP