0

I really want to avoid looping for this simple problem...

import numpy as np

x = np.array([[1,2,3,4], [5,6,1,2], [7,4,9,1]])
y = np.array([[2,5,6,7], [1,2,3,4], [1,2,3,4]])
print(x)
[[1 2 3 4]
 [5 6 1 2]
 [7 4 9 1]]

maxidx = np.argmax(x, axis=0)
print(maxidx)
[2 1 2 0]

So far so good. Now all I want it the entries in the y array for these indices. Since I get the index for each column only, I am not sure how to apply this correctly without looping or creating a list...thanks! 😊

2
  • 1
    What's your expected output? Commented Aug 24, 2015 at 9:44
  • The output should be [1 2 3 7] Commented Aug 24, 2015 at 9:49

1 Answer 1

2

Use multidimensional-indexing:

>>> indices = np.argmax(x, axis=0)
>>> y[indices, np.arange(x.shape[1])]
array([1, 2, 3, 7])
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.