如何在 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 程式碼。

更新於: 2023年10月10日

394 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.