Find Indices of Maximum and Minimum Value of Matrix in MATLAB
Last Updated :
28 Apr, 2025
Matrices in MATLAB are 2-dimensional arrays that store mostly numeric data at different indices. Now, to find the indices of maximum and minimum values of a given matrix, MATLAB does not provide any direct functionality however, we can do the same by using two other functionalities. Firstly, we will find the maximum or minimum value of a given matrix and then, we will find the indices of those two values. In this scenario, MATLAB does offer simple functions to perform the former tasks. In this article, we shall see how to do the same for a magic square.
Maximum and Minimum Values in a Matrix:
The max() and min() functions find the maximum and minimum values respectively in an array, along a given dimension. The output of these commands will be a row vector(default) which will have max/min values of each column in that array/matrix. Then we can apply the max()/min() function again to find the max/min values from that 1D vector.
Syntax:
To get row vectors with extreme values
max-row = max(matrix)
min-row = min(matrix)
To get extreme value from a given row vector of extreme values
max(max-row)
min(min-row)
Now let us see the same in action.
Maximum Value:
We will create a 5x5 magic square and find its maximum value, which should be 25.
Example 1:
Matlab
% MATLAB code
matrix = magic(5)
% Nesting the max command
% for finding maximum value
max_val = max(max(matrix))
Output:
Minimum Value:
Similarly, we will now find the minimum value of the same magic square, which should be 1.
Example 2:
Matlab
% MATLAB code for find min_val
matrix = magic(5)
min_val = min(min(matrix))
Output:
Finding Indices of Max/Min Values in the Same Magic Square:
Now we will use the find() function to get the indices of the max/min values.
Syntax:
max-index = find(matrix==max_val)
min-index = find(matrix==min_val)
Example 3:
Matlab
% Finding Indices of Max/Min
% Values in the Same Magic Square
matrix = magic(5);
% Getting max and min values
min_val = min(min(matrix));
max_val = max(max(matrix));
% Getting indices of min and max values in (row, col)
[minx,miny] = find(matrix==min_val);
[maxx,maxy] = find(matrix==max_val);
% Displaying the indices
fprintf("minimum index")
disp([minx,miny])
fprintf("maximum index")
disp([maxx,maxy])
Output:
Finding Max/Min Values With Multiple Occurrences:
We can also find the indices of all occurrences of the max/min value of a matrix in a similar way. See the following code for understanding the same.
Example 4:
Matlab
% Finding Max/Min Values With Multiple Occurrences
matrix = [1 2 3;
1 23 4;
2 23 5]
%Getting values
min_val = min(min(matrix));
max_val = max(max(matrix));
% Getting indices as vectors
% All of minx, miny, maxx, maxy are vectors
[minx,miny] = find(matrix==min_val);
[maxx,maxy] = find(matrix==max_val);
% Displaying the values
fprintf("minimum index\n")
disp([minx,miny])
fprintf("maximum index\n")
disp([maxx,maxy])
Output:
As can be seen, the minimum value 1 occurs at (1,1) and (2,1), and the maximum value 23 occurs at (2,2) and (3,2) indices. The same results are given by the above code.
Similar Reads
Min, Max and Mean of Off-Diagonal Elements in a Matrix in R A matrix is a combination of elements stacked together in either row or column format. A table-like structure formed of similar data type elements is known as a matrix. A matrix has two diagonals, one of which is known as the main diagonal. The main diagonal elements are characterized by the proper
3 min read
Find Column with Maximum Zeros in Matrix Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1. Examples: Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1},
5 min read
Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu
4 min read
PHP Program to Find the Maximum Element in a Matrix Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found. Example: Input: matrix = [ [1, 2, 3],
4 min read
Extract Values Above Main Diagonal of a Matrix in R In this article, we are going to know how to extract the values above the main diagonal of a matrix in the R programming language. The main diagonal of a matrix is a straight path that connects the entries (or elements) in a matrix whose row and column are the same. The number of elements in the mai
3 min read
How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi
4 min read