HansSnah
HansSnah

Reputation: 2250

numpy matrix minimum applied to second array

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! 😊

Upvotes: 0

Views: 43

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Use multidimensional-indexing:

>>> indices = np.argmax(x, axis=0)
>>> y[indices, np.arange(x.shape[1])]
array([1, 2, 3, 7])

Upvotes: 2

Related Questions