# To find the maximum value in a matrix?

3 229 views (last 30 days)
Let me have a 3X3 matrix
6 8 9
7 10 11
21 22 8
How to find the maximum value from this matrix?

Jan on 5 Sep 2012
Sorry that I mention the barely obvious, but the answer is 22.
KHOIROM Motilal on 17 Mar 2016
• 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 on 5 Sep 2012
Edited: Steven Lord on 25 Mar 2020
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 Comment

Image Analyst on 5 Sep 2012
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);

Azzi Abdelmalek on 5 Sep 2012
max(max(A))

Andrei Bobrov on 5 Sep 2012
M = [6 8 9
7 10 11
21 22 8];
index = 1;
max1 = M(index);
for jj = 2:numel(M)
if max1 < M(jj)
max1 = M(jj);
index = jj;
end
end
:)
José-Luis on 5 Sep 2012
Or if you really want to throw efficiency out the window:
M = [6 8 9
7 10 11
21 22 8];
unikM = unique(M);
val = unikM(1);
idx = unikM > val;
while (sum(idx) > 1)
idx = find(idx);
val = unikM(idx(1));
idx = unikM > val;
maxVal = unikM(idx)
end
Jonathan Posada on 20 Feb 2016
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 on 28 Jan 2020
M = max(A,[],'all') finds the maximum over all elements of A. This syntax is valid for MATLAB® versions R2018b and later.

John Doe on 31 Jan 2020
This should be upvoted and/or somehow appear closer to the chosen answer, as M = max(A,'all') seems not to work at all in R2018b+ (returning the entire matrix).
M = max(A(:)) seems to work in R2018b+ and presumably universally.
Steven Lord on 25 Mar 2020
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 on 22 Apr 2016
Edited: Dmaldo01 on 22 Apr 2016
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 on 16 May 2019
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

Steven Lord on 16 May 2019
No, you don't need to include multiple calls to max. See the accepted Answer for approaches that call max only once regardless of how many dimensions the input argument has.
Walter Roberson on 25 Mar 2020
Also it would only be n max calls.