user3685062
user3685062

Reputation: 79

MATLAB: finding a row index in a matrix

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.

Upvotes: 0

Views: 137

Answers (3)

rayryeng
rayryeng

Reputation: 104484

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.

Upvotes: 0

user3669833
user3669833

Reputation: 33

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    

Upvotes: 0

fiveclubs
fiveclubs

Reputation: 2431

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

Upvotes: 0

Related Questions