How can I find the max of two vectors?

7 次查看(过去 30 天)
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