필터 지우기
필터 지우기

How to return the row vector containing the highest values from a matrix?

조회 수: 1 (최근 30일)
Lewis
Lewis 2016년 11월 22일
댓글: Lewis 2016년 11월 22일
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
  댓글 수: 2
James Tursa
James Tursa 2016년 11월 22일
Please define "largest sequence" for us. What is the exact criteria you want for this? What would be returned for your example above?
Lewis
Lewis 2016년 11월 22일
편집: Image Analyst 2016년 11월 22일
Ah, the example isn't so good actually, but the largest sequence in that example would be
[10 9 11 7 8]
A better example might be:
M = [13 9 8 7 6;
13 12 11 9 8;
13 11 5 3 2]
Where I would want to return the following vector:
[13 12 11 9 8]
because if we were to sort all elements of each vector, then this one would contain the highest, the second highest, the third highest, etc.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2016년 11월 22일
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')
  댓글 수: 6
Image Analyst
Image Analyst 2016년 11월 22일
The second, optional output of max supplies the index from which the max value came from. Use it.
Lewis
Lewis 2016년 11월 22일
Thank you for the help - seems to be working perfectly now.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2016년 11월 22일
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
>>

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by