How can I find the max of two vectors?

11 visualizaciones (últimos 30 días)
Adrienne
Adrienne el 27 de Nov. de 2014
Comentada: Guillaume el 28 de Nov. de 2014
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

Respuesta aceptada

C.J. Harris
C.J. Harris el 27 de Nov. de 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 comentarios
C.J. Harris
C.J. Harris el 28 de Nov. de 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
Guillaume el 28 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

the cyclist
the cyclist el 27 de Nov. de 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.

Guillaume
Guillaume el 27 de Nov. de 2014
Editada: Guillaume el 27 de Nov. de 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)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by