在 MATLAB 中查詢矩陣的最大值和最小值的索引


在 MATLAB 中,矩陣是一個二維陣列,主要用於儲存數值資料。MATLAB 允許使用者對矩陣執行多種不同的數學運算。

本教程主要用於解釋兩個矩陣運算,即查詢矩陣的最大值和最小值的索引。但是,MATLAB 沒有提供任何直接方法來查詢矩陣的最大值和最小值的索引,但我們可以透過使用不同內建函式的組合來實現。

例如,要查詢矩陣的最大值的索引,我們可以使用 'max()' 和 'find()' 函式的組合。另一方面,如果我們想查詢矩陣的最小值的索引,則使用 'min()' 和 'find()' 函式的組合。

現在讓我們從實踐上了解如何在 MATLAB 中查詢矩陣的最大值和最小值的索引。

查詢矩陣的最大值的索引

在 MATLAB 中,我們可以使用兩個內建函式 'max' 和 'find' 的組合來查詢給定矩陣中最大值的索引。

示例

以下 MATLAB 程式演示了查詢矩陣中最大值索引的程式碼的實際實現。

% MATLAB program to find index of maximum value in matrix
% Create a sample matrix
M = [1 2 3 4 5 6; 5 4 8 2 7 3; 4 5 6 3 7 1; 8 3 6 7 9 2];

% Find the maximum value in the matrix M
Max_Value = max(M, [], 'all');

% Find the index of maximum value
I = find(M == Max_Value);

% Display the original matrix, maximum value, and its index
disp('Orignal Matrix:');
disp(M); 
disp('Maximum Value:'); 
disp(Max_Value); 
disp('Index of Maximum Value:'); 
disp(I)

輸出

Orignal Matrix:
     1     2     3     4     5     6
     5     4     8     2     7     3
     4     5     6     3     7     1
     8     3     6     7     9     2

Maximum Value:
     9

Index of Maximum Value:
    20

解釋

在此 MATLAB 程式碼中,我們首先建立一個示例矩陣 'M'。然後,我們使用 'max' 函式查詢矩陣中的最大值,並將其儲存在變數 'Max_Value' 中。

在 'max' 函式中,第一個引數 'M' 是矩陣,第二個引數 '[]' 表示在矩陣的所有值中找到最大值。第三個引數 'all' 指定必須考慮矩陣中所有值的最大值,而不管其排列方式如何。

之後,我們使用 'find' 函式查詢矩陣中最大值的索引。最後,我們使用 'disp' 函式顯示原始矩陣、最大值及其在矩陣中的索引。

查詢矩陣的最小值的索引

在 MATLAB 中,我們可以使用 'min' 和 'find' 函式的組合來查詢矩陣中最小值的索引。

示例

執行此操作的程式碼實現在以下 MATLAB 程式中進行了說明。

% MATLAB program to find index of minimum value in matrix
% Create a sample matrix
M = [1 2 3 4 5 6; 5 4 8 2 7 3; 4 5 6 3 7 1; 8 3 6 7 9 2];

% Find the minimum value in the matrix M
Min_Value = min(M, [], 'all');

% Find the index of maximum value
I = find(M == Min_Value);

% Display the original matrix, minimum value, and its index
disp('Orignal Matrix:');
disp(M); 
disp('Minimum Value:'); 
disp(Min_Value); 
disp('Index of Minimum Value:'); 
disp(I);

輸出

Orignal Matrix:
     1     2     3     4     5     6
     5     4     8     2     7     3
     4     5     6     3     7     1
     8     3     6     7     9     2

Minimum Value:
     1

Index of Minimum Value:
     1
    23

解釋

在此示例中,給定矩陣中的最小值為 '1',它存在於兩個位置 '1' 和 '23'。因此,程式的輸出。此 MATAB 程式碼的實現和執行與上一個程式碼相同。唯一的區別是,在此程式碼中,我們使用了 'min' 函式而不是 'max' 函式,因為我們需要查詢矩陣中最小值的索引。

我們還可以使用 'max' 和 'min' 的另一種語法來執行這些操作。這種新語法還消除了 'find' 函式的使用。

無需 'find' 函式查詢矩陣中最大值和最小值的索引

以下 MATLAB 示例程式碼說明了如何查詢矩陣中最大值和最小值的索引。

示例

% MATLAB code to find index of indices of maximum and minimum values in a matrix
% Create a sample matrix
M = [1 2 3 4 5 6; 5 4 8 2 7 3; 4 5 6 3 7 1; 8 3 6 7 9 2];

% Find the index of the maximum value
[Max_Value, Max_Index] = max(M(:));

% Find the index of the minimum value
[Min_Value, Min_Index] = min(M(:));

% Display the original matrix, maximum value & its index, and minimum value & its index
disp('Orignal Matrix:');
disp(M);
disp('Maximum Value:');
disp(Max_Value);
disp('Index of Maximum Value:');
disp(Max_Index);
disp('Minimum Value:');
disp(Min_Value);
disp('Index of Minimum Value:');
disp(Min_Index);

輸出

Orignal Matrix:
     1     2     3     4     5     6
     5     4     8     2     7     3
     4     5     6     3     7     1
     8     3     6     7     9     2

Maximum Value:
     9

Index of Maximum Value:
    20

Minimum Value:
     1

Index of Minimum Value:
     1

解釋

在此 MATLAB 程式碼中,我們使用帶 '(: )' 語法的 'max' 和 'min' 函式來查詢給定矩陣中的最大值和最小值。該語法具有以下優點:它同時返回矩陣中的值及其索引。因此,這消除了單獨查詢索引的 'find' 函式的使用。

結論

在本教程中,我們從實踐上演示瞭如何在給定矩陣中查詢最大值和最小值及其索引。MATLAB 沒有提供任何直接方法或函式來執行此操作,但我們可以使用不同函式的組合來執行該操作。我們討論了兩種查詢矩陣中最大值和最小值索引的不同方法。

更新於:2023 年 9 月 7 日

2K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.