How can I find the max of two vectors?

조회 수: 10 (최근 30일)
Adrienne
Adrienne 2014년 11월 27일
댓글: Guillaume 2014년 11월 28일
a=[4.84 3.55 2.09 4.20 1.14 5.15 2.45 3.41 1.66 2.75 3.87]
b=[ 7 8 8 7 7 9 8 6 5 7 8]
I want to find the max such that the max(a) is in the same column as max(b). I want to find the top 3. First should be column 6 Second should be column 11 Third should be column 1

채택된 답변

C.J. Harris
C.J. Harris 2014년 11월 27일
Do you actually want to sort by sum of columns?
For example:
a = [4.84 3.55 2.09 4.20 1.14 5.15 2.45 3.41 1.66 2.75 3.87];
b = [ 7 8 8 7 7 9 8 6 5 7 8];
[~,idx] = sort(a+b,'descend');
nTopThree = [a(idx(1:3));b(idx(1:3))];
nTopThree =
5.1500 3.8700 4.8400
9.0000 8.0000 7.0000
  댓글 수: 4
C.J. Harris
C.J. Harris 2014년 11월 28일
This answer is not completely wrong as you suggest. It does exactly what I said it does; it sorts by the total sum of the columns, as your example proves.
I think maybe you just misunderstood the question Guillaume.
Guillaume
Guillaume 2014년 11월 28일
I'm fairly certain that Adrienne was looking for something completely different (like a multikey sort or something else). As I said, in that case, the answer is completely wrong.
Yes, I may have misunderstood the question, and if Adrienne wanted a sort based on column sums your solution is indeed right, but as I also said, the question was certainly asked in a convoluted way.
In the end, we will never know until Adrienne comes back and tell us.

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

추가 답변 (2개)

the cyclist
the cyclist 2014년 11월 27일
I don't fully understand what you want to do. But I think you can do what you need with the sort and max commands. Note in particular that second output of the sort command that gives the sorting index.

Guillaume
Guillaume 2014년 11월 27일
편집: Guillaume 2014년 11월 27일
Sounds like you want to perform a sort on multiple keys. sortrows is the function for that. As it operates on row rather than columns, you have to transpose your data:
a = [4.84 3.55 2.09 4.20 1.14 5.15 2.45 3.41 1.66 2.75 3.87];
b = [ 7 8 8 7 7 9 8 6 5 7 8];
ab = [a; b]';
[c, col] = sortrows(ab, [2 1]); %sort on b first, then on a
%to get order by max, flip the results up/down
c = flipud(c);
col = flipud(col);
%to get the the first column for each unique value of b:
colmax = col(diff([NaN; c(:, 2)]) ~= 0)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by