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:
Find Indices of Maximum and Minimum Value of Matrix in MATLAB
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.