How can I find the max of two vectors?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
C.J. Harris
le 27 Nov 2014
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 commentaires
C.J. Harris
le 28 Nov 2014
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
le 28 Nov 2014
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.
Plus de réponses (2)
the cyclist
le 27 Nov 2014
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.
0 commentaires
Guillaume
le 27 Nov 2014
Modifié(e) : Guillaume
le 27 Nov 2014
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)
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!