0

want to sort np.ndarray indexes of an array such as

[[.5,.7, .9], [.6, .0, .8]]

result would look like this

[[1,1],[0,1],[1,0],[0,1],[1,2],[0,3]]

applying those indexes will get correct sorting order and at same time can be applied to other structures that match the data.

I tried np.argsort, but that doesn't give indexes for ndarray

1 Answer 1

1

You can use np.argsort on the flat array and then use np.divmod to get the indexes of your previous shape. Edit: np.unravel_index is the divmod alternative for higher dimensions, see https://numpy.org/doc/stable/reference/generated/numpy.unravel_index.html

Sign up to request clarification or add additional context in comments.

2 Comments

I did zip(*np.unravel_index(np.argsort(arr, axis=None), arr.shape)) to get that output. Thank you
You could also cast the unraveled output to an array and use the transpose instead of zipping.

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.