MATLAB Answers

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

10,504 ビュー (過去 30 日間)
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.

採用された回答

MathWorks Support Team
MathWorks Support Team 2020 年 11 月 12 日
編集済み: MathWorks Support Team 2020 年 11 月 12 日
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)

  4 件のコメント

表示 1 件の古いコメント
Chirag Parekh
Chirag Parekh 2016 年 12 月 29 日
You can use find command with max
find(A == max(A(:)))
Oscar Espinosa
Oscar Espinosa 2020 年 3 月 28 日
I got two indices, both have the same value. How can I choose just one of them?
Sergio Novi Junior
Sergio Novi Junior 2020 年 6 月 5 日
Oscar, it will depend on your application. For example, suppose you are looking for a car with the highest final speed. You find that there are two cars that have the best results. You can choose the car with lowest price.

サインインしてコメントする。

その他の回答 (9 件)

Shakir Kapra
Shakir Kapra 2015 年 4 月 20 日
編集済み: Shakir Kapra 2015 年 4 月 20 日
[M,I] = min(A)
where M - is the min value
and I - is index of the minimum value
Similarly it works for the max

  2 件のコメント

Shahab Forghani
Shahab Forghani 2020 年 8 月 4 日
Five years later, but thank you for this Mr. Kapra

サインインしてコメントする。


indrajit das
indrajit das 2016 年 8 月 3 日
find(arr == max(arr));

  0 件のコメント

サインインしてコメントする。


ANKUR KUMAR
ANKUR KUMAR 2017 年 9 月 19 日
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

  4 件のコメント

表示 1 件の古いコメント
amin nazari
amin nazari 2019 年 7 月 17 日
Thanks. very usefull
Anup Negi
Anup Negi 2020 年 9 月 6 日
how to use this code after saving the file , i am bignner of code
Steven Lord
Steven Lord 2020 年 9 月 6 日
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.)

サインインしてコメントする。


Roos
Roos 2018 年 5 月 10 日
編集済み: Roos 2018 年 5 月 10 日
https://www.mathworks.com/matlabcentral/answers/100813-how-do-i-find-the-indices-of-the-maximum-or-minimum-value-of-my-matrix#answer_282157
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.

  2 件のコメント

Erica Kiderman
Erica Kiderman 2018 年 5 月 15 日
Thank you for your feedback. The answer above has been corrected to address your comments.
Stanley Tam
Stanley Tam 2020 年 10 月 16 日
Above: [x,y]=find(A=minimum);
"=" should be replaced with "==", i.e. [x,y]=find(A == minimum);

サインインしてコメントする。


Andrew Teixeira
Andrew Teixeira 2019 年 10 月 1 日
How about just:
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 件のコメント

サインインしてコメントする。


Konstantinos Fragkakis
Konstantinos Fragkakis 2018 年 8 月 27 日
編集済み: Konstantinos Fragkakis 2018 年 8 月 27 日
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 件のコメント

Ammaa AlSada
Ammaa AlSada 2018 年 12 月 15 日
find the min(or max) of the 2nd row of an unkown matrix?
how to solve it

サインインしてコメントする。


Juanith HL
Juanith HL 2019 年 10 月 8 日
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 件のコメント

Martin Grden
Martin Grden 2020 年 10 月 12 日
Like this one best. It should be more efficient than those traversing the matrix two times.

サインインしてコメントする。


Samar Shahin
Samar Shahin 2020 年 5 月 2 日
[row,col]=find( matrix_name == min(matrix_name)) returns the indices of the minimum value in each coloumn in your matrix

  0 件のコメント

サインインしてコメントする。


SHIVAM SUNDRIYAL
SHIVAM SUNDRIYAL 2020 年 5 月 18 日
[M,I] = max(___) returns the index into the operating dimension that corresponds to the maximum value of A for any of the previous syntaxes.
Here M will represent and hold the maximum value while Iwill hold the index of the maximum value
you can use the same method to find the minimum value and its index by using the min() functiobn
Example :
A = [1 9 -2; 8 4 -5]
[M,I] = max(A)
Output:
M = 1×3
8 9 -2
I = 1×3
2 1 1

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by