Borrar filtros
Borrar filtros

sort a 2x4 matrix

1 visualización (últimos 30 días)
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 9 de Mayo de 2019
Editada: Adam Danz el 9 de Mayo de 2019
I have 2x4 matrix and would like to sort the first row from smallest number to largest number, but I would also like the second row to follow along.
V=[9 3 5 2; 30 49 22 354]
Iwant=[2 3 5 9; 354 49 22 30]

Respuesta aceptada

Adam Danz
Adam Danz el 9 de Mayo de 2019
[~, sortIdx] = sort(V(1,:));
Iwant = V(:,sortIdx);
  2 comentarios
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 9 de Mayo de 2019
Thank you! Just what I needed.
Adam Danz
Adam Danz el 9 de Mayo de 2019
Editada: Adam Danz el 9 de Mayo de 2019
Glad I could help!
Star Strider also had a good idea to use sortrows(). I did a speed test between these two solutions and found that the one I proposed is ~2.7 times faster on average (100,000 repetitions of each; p<0.0001 wilcox rank sum test).

Iniciar sesión para comentar.

Más respuestas (1)

Davide Figoli
Davide Figoli el 9 de Mayo de 2019
you could try doing:
V=[9 3 5 2; 30 49 22 354];
V_orderd=[];
V_ordered(1,:)=sort (V(1,:));
V_ordered(2,:)=sort(V(2,:),'descend');

Categorías

Más información sobre Shifting and Sorting 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