MATLAB Answers

To find the maximum value in a matrix?

조회 수: 5,783(최근 30일)
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2012년 9월 5일
답변: Sneha Baranwal 2021년 8월 16일
Let me have a 3X3 matrix
6 8 9
7 10 11
21 22 8
How to find the maximum value from this matrix?
  댓글 수: 2
KHOIROM Motilal
KHOIROM Motilal 2016년 3월 17일
  • clc
  • close all
  • clear all
  • X=[99 67 65;
  • 63 62 61;
  • 41 40 9];
  • MAX=X(1,1);
  • for i=1:3
  • for j=1:3
  • if MAX<= X(i,j);
  • MAX=X(i,j);
  • end
  • end
  • end
  • disp(MAX)

댓글을 달려면 로그인하십시오.

채택된 답변

Michael Völker
Michael Völker 2012년 9월 5일
편집: Steven Lord 2020년 3월 25일
Starting in R2018b, you can use the following command to find the maximum over all elements in an array A:
M = max(A, [], 'all');
For previous releases, use:
M = max(A(:));
  댓글 수: 1
Image Analyst
Image Analyst 2012년 9월 5일
To get it's location as well, accept both outputs of max:
[maxValue, linearIndexesOfMaxes] = max(A(:));
Note that there can be the max value at more than one location. To get the rows and columns (instead of linear indexes), you can use ind2subs() or find():
[rowsOfMaxes colsOfMaxes] = find(A == maxValue);

댓글을 달려면 로그인하십시오.

추가 답변(6개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 5일
max(max(A))
  댓글 수: 3
Jonathan Posada
Jonathan Posada 2016년 2월 20일
This works for the 2D case but if ndims(A)>2, then max(max(A)) will return a matrix. I believe OP wants the maximum element along all dimensions

댓글을 달려면 로그인하십시오.


Tom
Tom 2020년 1월 28일
M = max(A,[],'all') finds the maximum over all elements of A. This syntax is valid for MATLAB® versions R2018b and later.
  댓글 수: 2
Steven Lord
Steven Lord 2020년 3월 25일
The [] as the second input is required when you want to specify a dimension, including 'all'. The function call max(A, 'all') only works if A and 'all' are compatibly sized.
>> max(1:3, 'all')
ans =
97 108 108
>> max(1:3, [], 'all')
ans =
3

댓글을 달려면 로그인하십시오.


Dmaldo01
Dmaldo01 2016년 4월 22일
편집: Dmaldo01 2016년 4월 22일
This will work for all dimensions. If more efficient than ind2sub for less than 16000 elements.
[M,Index] = maxEl(MatVar)
index = size(MatVar);
Index = index*0;
M = max(MatVar(:));
A = find(MatVar==max(MatVar(:)),1);
for i = 1:length(index)
Index(i) = mod(ceil(A),index(i));
A = A/index(i);
end
Index(Index==0)=index(Index==0);

Yokesh
Yokesh 2019년 5월 16일
If matrix dimension is 'n', then max element can be found by:
max(max(.....maxn^2((A))...)
We have to include n^2 times max
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 3월 25일
Also it would only be n max calls.

댓글을 달려면 로그인하십시오.


JPS
JPS 2021년 2월 6일
or you can use,
M = max(max(A));
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 3월 15일
A = [1 3 2 5; 7 9 12 8; 12 8 9 0]
A = 3×4
1 3 2 5 7 9 12 8 12 8 9 0
[best3, best3idx] = maxk(A(:),3)
best3 = 3×1
12 12 9
best3idx = 3×1
3 8 5
The three maximum values are 12, 12, and 9, not 12, 9, and 8. If you are interested in the three maximum unique values, then you need to explicitly take into account that some values occur more than once.
k = 3;
uA = unique(A, 'sorted');
nresults = min(length(uA), k);
results = cell(nresults, 1);
for K = 1 : k
this_max = uA(end-K+1);
results{K,1} = this_max;
results{K,2} = find(A==this_max).';
end
disp(results)
{[12]} {[ 3 8]} {[ 9]} {[ 5 9]} {[ 8]} {[6 11]}
The output is a cell array, in which the first column gives you the value that is the maximum, and the second column gives you all the linear indices into the array. The code could be modified to give row and column outputs instead without much change.
The code does not assume that the number of occurrences is the same for each of the values (if that were known to be true then a numeric array could be created instead of a cell array.)

댓글을 달려면 로그인하십시오.


Sneha Baranwal
Sneha Baranwal 2021년 8월 16일
k = 3;
uA = unique(A, 'sorted');
nresults = min(length(uA), k);
results = cell(nresults, 1);
for K = 1 : k
this_max = uA(end-K+1);
results{K,1} = this_max;
results{K,2} = find(A==this_max).';
end
disp(results)

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by