Borrar filtros
Borrar filtros

Sort a vector according to another vector

32 visualizaciones (últimos 30 días)
Mohsen Rajaei
Mohsen Rajaei el 4 de Mzo. de 2014
Editada: JONATAN RODRIGUEZ VAZQUEZ el 13 de Jun. de 2021
Hi
I have tow vectors A and B:
A = [4 8 5 9 1]
B = [3 5 4 2 1]
If I sort A with B like this:
C = A(B)
I will get:
C = [5 1 9 8 4]
But I want C like this:
C = [1 9 4 5 8]
How can I do that?
Thank you, M. Rajaei

Respuesta aceptada

lvn
lvn el 4 de Mzo. de 2014
[~,Bsort]=sort(B); %Get the order of B
C=A(Bsort)
C =
1 9 4 5 8
  2 comentarios
Mohsen Rajaei
Mohsen Rajaei el 4 de Mzo. de 2014
OK. Thank you so much. I got it.
JONATAN RODRIGUEZ VAZQUEZ
JONATAN RODRIGUEZ VAZQUEZ el 13 de Jun. de 2021
Editada: JONATAN RODRIGUEZ VAZQUEZ el 13 de Jun. de 2021
how can you do the same thing with n x 2 array?
I do it like this
vectors = [3 4;5 12];
modules = [5;13];
[~,modulesSort] = sort(modules); %Get the order of B
%C=A(modulesSort); %not works
% do this
vectorsSortedByModule = vectors(modulesSort,:);

Iniciar sesión para comentar.

Más respuestas (0)

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