1

How do I reorganize elements of A (shape = (1,7,2)) to generate new array A1 (shape = (1,7,2)) in order of increasing j? In A[0,0]= array([0, 1]), j refers to 1 in [0,1].

It would be great to have something very generic so that the code can deal with when shape of A becomes very large, say (1,1000,2).

import numpy as np

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 5],[3, 4],[4, 7],[5, 6]]])
print("A =", A.shape)

The desired output is

A1 = np.array([[[0, 1],[0, 2],[1, 3],[3, 4],[2, 5],[5, 6],[4, 7]]])
1
  • 3
    np.sort(A) The last axis is the default. Or, you could use np.sort(A, axis=2). Commented Jun 26, 2022 at 6:04

1 Answer 1

2

The order is defined first, then the array is sorted by order.

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 5],[3, 4],[4, 7],[5, 6]]])
order = A[0,:, 1].argsort()
A =np.array([A[0][order]])
print(A)
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.