MATLAB: Keeping track of the original indices of an array after sorting

arraysindicessort

I have an array let's say A=[5 4 1 2 3]. Now I have sorted these arrays in ascending order.So the resulting array will now be [1 2 3 4 5]. I use sort(A,'ascend') function to sort this. The question is how can I get the indices of the original array with respect to my new array. originally my indices were 1,2,3,4,5 for corresponding values of 5,4,1,2,3 and now the indices have changed to 3,4,5,2,1. How can I get these indices efficiently in Matlab?

Best Answer

Md, simply use
[B,I] = sort(A,'ascend')
B =
1 2 3 4 5
I =
3 4 5 2 1