MATLAB Answers

How to return entire elements of a column of a matrix while that column includes maximum value of among all other elements of matrix

2 views (last 30 days)
Milad Naderloo
Milad Naderloo on 27 Feb 2021
Commented: dpb on 27 Feb 2021
Here it is my question:
I have follwoing 3*8 matrix:
A =
0.0596 0.0714 0.8181 0.1499 0.9730 0.4538 0.0835 0.3909
0.6820 0.5216 0.8175 0.6596 0.6490 0.4324 0.1332 0.8314
0.0424 0.0967 0.7224 0.5186 0.8003 0.8253 0.1734 0.8034
What I want is finding the column which includs maximum value among all other elements of matrix and then returning all other elemnts of that column.
Considering my description ad question following matrix B (3*1) would be the answer which returns the all elemnts of fifth column which includs maximum value.
B =
Can you please help me to write the script for that?
Thnaks in advance!
dpb on 27 Feb 2021
IDS -- the user names of regulars here -- you used their names as Tags instead of something subject-related to your question, the whole point of there being tags.
Who provides a useful answer is immaterial and trying to attract somebody's attention to your specific Q? is not proper etiquette.

Sign in to comment.

Accepted Answer

dpb on 27 Feb 2021
[~,ix]=max(max(A)); % return column of max of 2D array
B=A(:,ix); % the resulting column
Too bad there's no way to return the alternate return values as function result or could manage in one line/anonymous function. MATLAB syntax here requires the temporary variable.
NB: max(max()) uses default behavior of max (and most other MATLAB functions) to operate by column by default -- hence the inside max() returns a row vector of column max'es; the second then returns the location of the max in that vector--the desired column of the overall max in the array.
One could find the overall maximum location via
>> [~,i]=max(A,[],'all','linear')
i =
>> [~,i]=max(A(:))
i =
but one then would need to convert that linear location back to row, column by ind2sub
>> [r,c]=ind2sub(size(A),i)
r =
c =
requiring yet another temporary since the column is the second output form ind2sub

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by