0

Hello I'm trying to do the following.

I have a matrix

a = np.array([[1,4,3], [0,3,7]])

I would like to transform the matrix so each cell new_a[i,j] contains the order (descending order) of the a[i,j] along the axis=1 So the result would be

a_new = np.array([[2,0,1], [2,1,0]])

I tried this (it works)

argsorts = np.argsort(a, axis=1) 

for i in a.shape[0]:
    a[i, argsorts[i]] = range(a.shape[1])[::-1]

I would like to change the for loop by something else Thank you

1 Answer 1

0

Try

>>> 2-a.argsort()
[[2 0 1]
 [2 1 0]]
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.