Find Indices of Maximum and Minimum Value of Matrix in MATLAB


In MATLAB, a matrix is a 2D array that is mainly used store numerical data. MATLAB allows users to perform several different types of mathematical operations on matrices.

This tutorial is primarily meant for explaining two matrix operations namely, finding indices of maximum and minimum elements of the matrix. However, MATLAB does not provide any direct method for finding the indices of maximum and minimum value of a matrix, but we can do this by using a combination of different built-in functions.

For example, to find the index of maximum value of matrix, we can use a combination of the 'max()' and 'find()' functions. On the other hand, if we want to find the index of minimum value of a matrix, then we use the combination of the 'min()' and 'find()' functions.

Let us now practically understand how to find the indices of maximum and minimum value of a matrix in MATLAB.

Find the Index of Maximum Value of a Matrix

In MATLAB, we can use the combination of two built-in functions 'max' and 'find' to find the index of the maximum value in the given matrix.

Example

The following MATLAB program demonstrates the practical implementations of codes to find the index of maximum value in a matrix.

% 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)

Output

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

Explanation

In this MATLAB code, we start by creating a sample matrix 'M'. Then, we use the 'max' function to find the maximum value in the matrix and store it in a variable 'Max_Value'.

In the 'max' function, the first argument 'M' is the matrix, the second argument '[]' represents that the maximum value is found across all the values in the matrix. The third parameter 'all' specifies that the maximum value must be considered over all values in the matrix, regardless of their arrangement.

After that we use the 'find' function to find the index of the maximum value in the matrix. Finally, we use the 'disp' function to display the original matrix, the maximum value, and its index in the matrix.

Find the Index of Minimum Value of a Matrix

In MATLAB, we can use the combination of 'min' and 'find' functions to find the index of minimum value in a matrix.

Example

The code implementation to perform this operation is illustrated in the following MATLAB program.

% 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);

Output

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

Explanation

In this example, the minimum value in the given matrix is '1' which exists at two positions '1' and '23'. Hence, the output of the program. The implementation and execution of this MATAB code is same as that of the previous one. The only difference is that in this code we have used the 'min' function instead of the 'max' function, as we need to find the index of minimum value in the matrix.

We can also perform these operations using another syntax of 'max' and 'min'. This new syntax also eliminates the use of the 'find' function.

Find Indices of Maximum and Minimum Values in a Matrix without 'find' Function

The following MATLAB example code illustrates how to find the indices of maximum and minimum values in a matrix.

Example

% 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);

Output

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

Explanation

In this MATLAB code, we use the 'max' and 'min' functions with the '(:)' syntax to find the maximum and minimum values in the given matrix. The syntax has the advantage that it returns both the value and its index in the matrix. Hence, this eliminates the use of the 'find' function to find the index separately.

Conclusion

In this tutorial, we practically demonstrated how to find the maximum and minimum values and their indices in a given matrix. MATLAB does not provide any direct method or function to perform this operation, but we can use a combination of different functions to perform the operation. We discussed two different approaches to find the indices of maximum and minimum values in a matrix.

Updated on: 07-Sep-2023

634 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements