Position of max value of each column in a matrix
Show older comments
I have several large MxN matrices that I need to find the POSITION (specifically the y value or row is more important) of the max value of EACH column in the matrix. So for instance if A = [1,4,9; 2,8,7; 3,4,5],then the script would return m = [3,2,1] and n = [1,2,3], where m is the row and n is the column.
I can find the maximum number and its position in the matrix, I can find the maximum number and position in a particular column, but I can't seem to automate it to find and store the values for each column.
I tried a couple different things, and I thought I had it with this:
for i = 1:S %S is the the # of columns
[num] = max(A(:,i));
[m,n] = ind2sub(size(A), find(A==num));
end
but this only returns the coordinates for the last column. So am I missing something obvious here? Is here anything better? Thanks for any input
Answers (1)
Azzi Abdelmalek
on 21 Mar 2014
Edited: Azzi Abdelmalek
on 21 Mar 2014
A = [1,4,9; 2,8,7; 3,4,5];
[~,ii]=max(A);
m=ii
n=1:size(A,2)
1 Comment
Ricardo Gutierrez
on 16 Jan 2018
Excelente!!!!! Excellent !!!!!!
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!