1

I have two 2D arrays x and y.

x = np.array([[2,4,6],
              [9,4,6],
              [6,8,3]])

y = np.array([[88,55,33],
              [43,87,65],
              [98,34,56]])

Using the argmax function, I found the indices of the maximum values of x along axis 1.

idx = x.argmax(axis=1)

output: array([2, 0, 1], dtype=int64)

now, I want the the values from array y, which are on these specific indices. My expected array is ([33,43,34]).

I tried using y[idx] but it gives the following output.

array([[98, 34, 56],
   [88, 55, 33],
   [43, 87, 65]])

How can I get the output I want?

1 Answer 1

1

Easy as using np.arange :

result = y[np.arange(len(idx)), idx]
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.