user552231
user552231

Reputation: 1135

Getting the indices of the max values of matrix columns in MATLAB

I need to get the indices of the maximum values of the columns in a matrix, for example:

a =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1  

and I want to get

[1, 4, 4, 1] 

which are the indices of 16,14,15,13 i.e. the maximum value in each column. I discovered that

max(a,[],1) 

returns

[16, 14, 15, 13]  

How can I get their indices?

Upvotes: 3

Views: 2824

Answers (1)

Phonon
Phonon

Reputation: 12737

You need to find indices, not the numbers themselves, so you need the second output argument.

[~,I] = max(a)

Upvotes: 3

Related Questions