Asked by Lewis
on 22 Nov 2016

Hi, I can't seem to find a solution to this but it's a pretty simple problem. Say I have a matrix:

M = [10 9 11 7 8;

10 9 8 7 6;

9 6 7 8 5]

What I want to do is to somehow extract the row vector which holds the largest sequence. I have tried the max() function but it just doesn't retain the individual rows. It is important that it returns a specific row vector from M.

The rows won't always necessarily be sequential like this, so I cannot just use the row that sums to the largest value either.

Thanks

Answer by Image Analyst
on 22 Nov 2016

Accepted Answer

Try this:

M = [13 9 8 7 6;

13 12 11 9 8;

13 11 5 3 2]

% Find the maxima in each column independently.

columnMaxima = max(M, [], 1)

% Now sort in decending order.

columnMaxima = sort(columnMaxima, 'Descend')

Lewis
on 22 Nov 2016

If possible then just taking the first row should be fine... I'd quite like to keep an index of which row from M it came from.

The same issue also occurs in the following example:

M = [9 6 7 5 8; 6 7 5 8 4];

columnMaxima = max(M, [], 1);

columnMaxima = sort(columnMaxima, 'Descend')

columnMaxima = [9 7 7 8 8]

Image Analyst
on 22 Nov 2016

The second, optional output of max supplies the index from which the max value came from. Use it.

Lewis
on 22 Nov 2016

Thank you for the help - seems to be working perfectly now.

Sign in to comment.

Answer by dpb
on 22 Nov 2016

Sort first by row (already done here in these examples), then by column, retaining the sort index. Then (I think, altho didn't prove concluseively) the row will be minimum of the sum of those indices (fewest permutations to arrange).

>> [~,ix]=sort(M,1);

>> [~,ix]=min(sum(ix,2))

>> M(ix,:)

ans =

13 12 11 9 8

>>

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## James Tursa (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/313521-how-to-return-the-row-vector-containing-the-highest-values-from-a-matrix#comment_408298

## Lewis (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/313521-how-to-return-the-row-vector-containing-the-highest-values-from-a-matrix#comment_408309

Sign in to comment.