 ## How do I find the indices of the maximum (or minimum) value of my matrix?

### MathWorks Support Team (view profile)

on 7 Oct 2009
Latest activity Answered by Juanith HL

### Juanith HL (view profile)

on 8 Oct 2019
Accepted Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

The 'find' command only returns the indices of all the non-zero elements of a matrix. I would like to know how to find the indices of just the maximum (or minimum) value.

### Products ### MathWorks Support Team (view profile)

on 14 Mar 2019
Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

on 14 Mar 2019

The "min" and "max" functions in MATLAB return the index of the minimum and maximum values, respectively, as an optional second output argument.
For example, the following code produces a row vector 'M' that contains the maximum value of each column of 'A', which is 3 for the first column and 4 for the second column. Additionally, 'I' is a row vector containing the row positions of 3 and 4, which are 2 and 2, since the maximums for both columns lie in the second row.
A = [1 2; 3 4];
[M,I] = max(A)
For more information on the 'min' and 'max' functions, see the documentation pages listed below:
To find the indices of all the locations where the maximum value (of the whole matrix) appears, you can use the "find" function.
maximum = max(max(A));
[x,y]=find(A==maximum)

Keqiao Li

### Keqiao Li (view profile)

on 22 Dec 2016
what if I have two max/min values in one matrix. What I mean is if the matrix is like A = [1,2,3;4,5,6;7,8,8]. In this case, obviously, the max value is 8, but there are two "8". What should I do? Thanks!
Chirag Parekh

### Chirag Parekh (view profile)

on 29 Dec 2016
You can use find command with max
find(A == max(A(:)))

### Shakir Kapra (view profile)

on 20 Apr 2015
Edited by Shakir Kapra

### Shakir Kapra (view profile)

on 20 Apr 2015

[M,I] = min(A)
where M - is the min value
and I - is index of the minimum value
Similarly it works for the max

#### 1 Comment

Mohammed ABDELAZIZ

on 28 Mar 2016
thnaks

### ANKUR KUMAR (view profile)

on 19 Sep 2017

Use this as a function and type [x,y]=minmat(A) to get the location of the minimum of matrix. for example:
>> A=magic(5)
>> [a,b]=minmat(A)
a =
1
b =
3
Save this as a function in your base folder and use it.
function [ a,b ] = minmat( c )
as=size(c);
total_ele=numel(c);
[~,I]=min(c(:));
r=rem(I,as(1));
a=r;
b=((I-a)/as(1))+1;
if a==0
a=as(1);
b=b-1;
else
a=r;
b=b;
end
end

Jia Li

### Jia Li (view profile)

on 9 Apr 2018
Thanks! It worked very well.
amin nazari

### amin nazari (view profile)

on 17 Jul 2019
Thanks. very usefull

on 10 May 2018
Edited by Roos

### Roos (view profile)

on 10 May 2018

This apparently solved your question, however for future reference I would like to mention that there is an earier solution that does not involve declaring a function.
Lets continue with any matrix A. The first step is finding the minimum value of the complete matrix with:
minimum=min(min(A));
The double min is needed to first find min of all columns, then find min of all those min values. (there might be an easier way for this as well).
Finding the indices of this value can be done like this:
[x,y]=find(A=minimum);
2 lines will be easier than a complete function.

Erica Kiderman

on 15 May 2018

### indrajit das (view profile)

on 3 Aug 2016

find(arr == max(arr));

### Andrew Teixeira (view profile)

on 1 Oct 2019

A = magic(5);
[Amins, idx] = min(A);
[Amin, Aj] = min(Amins);
Ai = idx(Aj);
where your final matrix minima is located at [Ai, Aj]

### Konstantinos Fragkakis (view profile)

on 27 Aug 2018
Edited by Konstantinos Fragkakis

### Konstantinos Fragkakis (view profile)

on 27 Aug 2018

Function to calculate the minimum value and its indices, in a multidimensional array - In order to find the max, just replace the min(array(:)) statement with max(array(:)).
function [ minimum,index ] = minmat( array )
% Function: Calculate the minimum value and its indices in a multidimensional array
% -------- Logic description --------
% First of all, identify the Matlab convention for numbering the elements of a multi-dimensional array.
% First are all the elements for the first dimension
% Then the ones for the second and so on
% In each iteration, divide the number that identifies the minimum with the dimension under investigation
% The remainder is the Index for this dimension (check for special cases below)
% The integer is the "New number" that identifies the minimum, to be used for the next loop
% Repeat the steps as many times as the number of dimensions (e.g for a 2-by-3-by-4-by-5 table, repeat 4 times)
neldim = size(array); % Length of each dimension
ndim = length(neldim); % Number of dimensions
[minimum,I] = min(array(:));
remaining = 1; % Counter to evaluate the end of dimensions
index = []; % Initialize index
while remaining~=ndim+1 % Break after the loop for the last dimension has been evaluated
% Divide the integer with the the value of each dimension --> Identify at which group the integer belongs
r = rem(I,neldim(remaining)); % The remainder identifies the index for the dimension under evaluation
int = fix(I/neldim(remaining)); % The integer is the number that has to be used for the next iteration
if r == 0 % Compensate for being the last element of a "group" --> It index is equal to the dimension under evaluation
new_index = neldim(remaining);
else % Compensate for the number of group --> Increase by 1 (e.g if remainder 8/3 = 2 and integer = 2, it means that you are at the 2+1 group in the 2nd position)
int = int+1;
new_index = r;
end
I = int; % Adjust the new number for the division. This is the group th
index = [index new_index]; % Append the current index at the end
remaining = remaining + 1;
end
end

#### 1 Comment

on 15 Dec 2018
find the min(or max) of the 2nd row of an unkown matrix?
how to solve it