How can I find the max of two vectors?

12 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by