yalpsid eman
yalpsid eman

Reputation: 3432

Get vector of columns with highest values in matrix

I'd like to be able to create a column vector whose values in each row correspond to the column in a matrix with the max value in that specific row.

For example, If I have a matrix such as:

A = [1,5,2;3,1,1;0,1,0];

I'd end up with the matrix:

maxValueColumns = transpose([2,1,2]);

Is there an easy/efficient way to do this?

Upvotes: 0

Views: 73

Answers (1)

frslm
frslm

Reputation: 2978

You're looking for max():

A = [1,5,2;3,1,1;0,1,0];
[~, maxValueColumns] = max(A, [], 2); % 'maxValueColumns' will contain [2; 1; 2]

Upvotes: 1

Related Questions