在 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.