Let two ndarrays: A of shape (n, *m), and B of shape (n, ). Is there a way to sort A in-place using the order that would sort B?
Sorting A with B is easy using np.argsort, but this is not done in-place:
A = A[np.argsort(B)]
Comments:
AandBhave different dtypes, andAcan have more than two dimensions. Hence they can’t be stacked to usendarray.sort().Atakes up a lot of space, which is why it needs to be sorted in-place. Any solution requiring twice the space occupied byAwould therefore defeat this purpose.- The title of this question “Re-arranging numpy array in place” may sound related, but the question itself is not very clear, and the answers do not match my question.
A[arr,:], indexing the rows ofA. That necessarily is a copy. Even if you useA[:] = ...to write the new values back 'on-to'A, there will be a temporary buffer with the data.