To find the maximum value in a matrix?

1,623 ビュー (過去 30 日間)
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2012 年 9 月 5 日
コメント済み: Steven Lord 2022 年 12 月 11 日
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 件のコメント
Jan
Jan 2012 年 9 月 5 日
Sorry that I mention the barely obvious, but the answer is 22.
KHOIROM Motilal
KHOIROM Motilal 2016 年 3 月 17 日
編集済み: 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(:));
  4 件のコメント
Stephen23
Stephen23 2022 年 12 月 11 日
Steven Lord
Steven Lord 2022 年 12 月 11 日
The max function's first two inputs are the two matrices whose values you want to compare. If you only want to compute the maximum of one matrix, you need something to use as a placeholder for that second matrix. Otherwise if you wrote something like this, are you asking for the maximum of the elements of a matrix and the value 1 or are you asking for the maximum along the 1st dimension?
max(A, 1)
To break that ambiguity, that syntax is interpreted as the former (the maximum of the elements of A and the value 1) while the following is the latter (the maximum along the 1st dimension.)
max(A, [], 1)

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

その他の回答 (5 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 5 日
  4 件のコメント
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
DGM
DGM 2022 年 12 月 11 日
Not that this is a good idea, but for an arbitrary number of dimensions:
A = rand(100,100,100,10); % a fairly large ND array
% find global maximum of A
maxval = max(A);
for n = 2:ndims(A)
maxval = max(maxval);
end
maxval
maxval = 1.0000
It hasn't been so for quite some time, but in my experience, this iterative approach had a significant speed advantage with larger N-D arrays in older versions (2x-3x as fast as max(A(:)) for the arrays I was using). I don't remember if that advantage still existed in R2012x, but it did in R2009b. In current versions, using vectorization or 'all' are faster for small arrays and roughly equivalent for large arrays. That's on my hardware, so I make no guarantees that it's exactly universal.
Performance aside, it's hard to justify this verbose method over the canonical techniques, if only for the sake of readability.
It's not something I'd recommend, and I doubt that the legacy performance is the typical reason that people gravitate to the approach, but I thought it was interesting to note for old time's sake.

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


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 件のコメント
John Doe
John Doe 2020 年 1 月 31 日
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
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 件のコメント
Steven Lord
Steven Lord 2019 年 5 月 16 日
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
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 件のコメント
Adrian Brown
Adrian Brown 2021 年 3 月 15 日
hello,
There is any way for a matrix size NxM to get the k maximum element in the whole matrix not in rows or colomns but in only elements. for example matrix A = [1 3 2 5, 7 9 12 8, 12 8 9 0] for K= 3 the 3 maximum elements are 12 9 and 8 and I want to return there location in the matrix.
I really appreciate any help
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.)

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

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by