0
votes

I have a matrix and I want to find the maximum value in each column, then find the index of the row of that maximum value.

3

3 Answers

0
votes
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

[~,colind] = max(max(A))

colind =

     3

returns colind as the column index that contains the maximum value. If you want the row:

[~,rowind] = max(A);
max(rowind)

ans =

    5
0
votes

You can use a fairly simple code to do this.

MaximumVal=0
for i= i:length(array)
   if MaximumVal>array(i)
      MaximumVal=array(i);
      Indicies=i;
   end
end
MaximumVal
Indicies    
0
votes

Another way to do this would be to use find. You can output the row and column of the maximum element immediately without invoking max twice as per your question. As such, do this:

%// Define your matrix
A = ...;

% Find row and column location of where the maximum value is
[maxrow,maxcol] = find(A == max(A(:)));

Also, take note that if you have multiple values that share the same maximum, this will output all of the rows and columns in your matrix that share this maximum, so it isn't just limited to one row and column as what max will do.