1

I have 2 numpy arrays:

A = np.array([6, 7 ,8 ,9, 0])
B = np.array([5, 3, 2, 4, 1])

And would like to reshuffle the first one using the second array. So the first element of A should go to place 5 in the output array. The second element should go to to the third etc. So the output array becomes:

C = np.array([0, 8, 7, 9, 6])

This is simple using a simply python loop, but I would like to use numpy only. Speed is very important.

1 Answer 1

3

Numpy allows you to use B to index A. You have to subtract 1 from B, because indices in numpy arrays begin at 0:

In [17]: A = np.array([6, 7, 8, 9, 0])

In [18]: B = np.array([5, 3, 2, 4, 1])

In [19]: C = A[B-1]

In [20]: C
Out[20]: array([0, 8, 7, 9, 6])
Sign up to request clarification or add additional context in comments.

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.