MATLAB: Sort a vector according to another vector

sort

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

Best Answer

[~,Bsort]=sort(B); %Get the order of B
C=A(Bsort)
C =
1 9 4 5 8