Finding the new order of shuffled array

Hello Julia users! I was having a problem while writing some code that I hope you can help with. So basically what I want to do is that I have an original array A of some length which contains some integers. This array has a corresponding order of the elements. If I then shuffle A I am interested in extracting the new order of the elements in a nice way. Perhaps the code makes it more clear:

A = [11,13,25,10,9]
#The corresponding order of this original A-vector would be:
Order = [1,2,3,4,5]
#If we then Shuffle our A-vector we should get a new vector:
A_Shuffled = shuffle(A)
#This eg. gives
A_Shuffled = [25,13,10,9,11]
#What I am interested in extracting is then the new order which should be:
Order_Shuffled = [3,2,4,5,1]

This is of course doable by using a for-loop and then finding the new indices of the elements in the original A-vector using findall, but I was wondering if there was a more clean approach to doing it. I couldn’t find anything by searching for it, so I hope you guys can help :slight_smile:

You can shuffle the indexes instead of the original array:

A = [11,13,25,10,9]
p = shuffle(eachindex(A)) # new order of elements
A_shuffled = A[p]

If you really want to find the index only after shuffling the original array, you can use indexin(A_shuffled, A).

3 Likes

Thank you :slight_smile: Solved my problem!