how to pinpoint the maximum scalar in a matrix?

3 ビュー (過去 30 日間)
Sameer
Sameer 2014 年 6 月 1 日
編集済み: Matt J 2014 年 6 月 1 日
how do we extract the maximum number from a matrix, and tell the screen which column or row this number is in?

回答 (2 件)

Geoff Hayes
Geoff Hayes 2014 年 6 月 1 日
One way is to use the max command (type help max) for details. Suppose you have the following matrix:
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
It is clear that the maximum is in the fifth row, third column. To find that maximum and which column it is in type:
[mxval,col] = max(max(A));
See the documentation on max as to why we do the max(max(A)). So if there is just the single maximum value (no duplicates) then you can do the following:
row = find(A(:,col)==mxval);
Then to display to the console/command window:
fprintf('max value=%f, row=%d, col=%d\n',mxval,row,col);
Note that you should write some code to handle the case where there are duplicates of the maximum value in the matrix (and so mxval and col are 1xN vectors, with N>1).

Matt J
Matt J 2014 年 6 月 1 日
編集済み: Matt J 2014 年 6 月 1 日
maxval=max(A(:));
[row,col]=find(A==maxval),

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by