How do I find the column of maximum values of a maxtrix?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ziq Kimi
am 27 Mär. 2020
Bearbeitet: Akira Agata
am 27 Mär. 2020
Let's say
x= [4, 90, 85, 75; 2, 55, 65, 75; 3, 78, 82, 79;...
1, 84, 92, 93]
x =
4 90 85 75
2 55 65 75
3 78 82 79
1 84 92 93
If using
[a, b]= max(x)
a will be the maximum values in each columns while b will be their position in terms of rows
I will get
a =
4 90 92 93
b =
1 1 4 4
If using
max(x, [], 'all')
I will get the highest value throughout the columns and rows which is 93.
Now, how do I find the column of the highest value (93) ? Maybe something like
[a, b]= max(x)
but the position/index is in terms of column.
0 Kommentare
Akzeptierte Antwort
Akira Agata
am 27 Mär. 2020
Bearbeitet: Akira Agata
am 27 Mär. 2020
If you want to find the column where the maximum value in 2D matrix locates, one simple way is:
[~,col] = max(max(x));
For example:
x= [4, 90, 85, 75; 2, 55, 65, 75; 3, 78, 82, 79;...
1, 84, 92, 93];
[~,col] = max(max(x));
>> col
col =
4
0 Kommentare
Weitere Antworten (1)
the cyclist
am 27 Mär. 2020
Here is one way to do it.
[~,linearIndex] = max(x,[],'all','linear'); % First, find the *linear* index of x
[rowIndex,colIndex] = ind2sub(size(x),linearIndex); % Find the row and col indices from the linear index
Note that the first line could also have used the syntax
[~,linearIndex] = max(x(:))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!