# Sorting Vector A in Ascending order and apply the same steps to Vector B

5 views (last 30 days)
Lachlan Martin on 28 Oct 2020
Answered: Sudhakar Shinde on 28 Oct 2020
Lets Say I have the vectors
A=[1 3 5 2 4]
B=[5 2 6 7 9]
I want to sort A in ascending order such that
A=[1 2 3 4 5]
But I also it this to happen to B such that the corresponding value is moved as well
B=[5 7 2 9 6]
So before and after sorting
1 still corresponds with 5
2 still corresponds with 7
3 still corresponds with 2
4 still corresponds with 9
5 still corresponds with 6
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Stephen23 on 28 Oct 2020
[A,X] = sort(A)
B = B(X)
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### More Answers (1)

Sudhakar Shinde on 28 Oct 2020
[m n]=sort(A);
disp(m); % Sorted A vector in ascending order
B=B(n);
disp(B);% Sorted B according to A.
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!