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

5.502 visualizzazioni (ultimi 30 giorni)
MathWorks Support Team il 7 Ott 2009
Commentato: Steven Lord il 18 Ago 2022
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.

### Risposta accettata

MathWorks Support Team il 28 Mar 2022
Modificato: MathWorks Support Team il 17 Nov 2021
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)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (5)

Shakir Kapra il 20 Apr 2015
Modificato: Shakir Kapra il 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

indrajit das il 3 Ago 2016
find(arr == max(arr));
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Andrew Teixeira il 1 Ott 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]
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Juanith HL il 8 Ott 2019
A = [8 2 4; 7 3 9]
[M,I] = max(A(:)) %I is the index maximun Here tu can change the function to max or min
[I_row, I_col] = ind2sub(size(A),I) %I_row is the row index and I_col is the column index
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Steven Lord il 18 Ago 2022
If you can use linear indices, instead I would recommend:
A = [8 2 4; 7 3 9];
[X, ind] = max(A, [], 'all', 'linear')
X = 9
ind = 6
This will tell us the maximum value in A is 9 and it is located in element 6 of A.

Accedi per commentare.

ANKUR KUMAR il 19 Set 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
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Steven Lord il 6 Set 2020
Once you've stored this code in a file named minmat.m (see this documentation page for the basics of defining a function) you can call this like any other MATLAB function (as shown on this other documentation page.)

Accedi per commentare.

### Categorie

Scopri di più su Manage Products in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by