0

Suppose i have two arrays a=[4 3 6 1 7 8] b=[3 5 1 8 4 6]

I need to sort b in the same order as a, which will be sorted in ascending order ie.,

first sorting a a=[1 3 4 6 7 8] whose corresponding indices will be [4 2 1 3 5 6] and using this indices, i have to sort b, ie., b=[8 5 3 1 4 6]

how do i do this in MATLAB

2 Answers 2

2

see the 2nd output of sort

[sortedArray,sortedIndex] = sort(a)
b(sortedIndex)
Sign up to request clarification or add additional context in comments.

Comments

1

The second output of sort describes the ordering of the elements. See doc sort

[aSrt, ind] = sort(a);
bSrt = b(ind);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.