4

Given an array of integers, I need to find the indexes of many of its elements stored in a different array. This is:

import numpy as np
a1 = np.array([ 4, 5, 6, 1, 2, 3, 7, 86, 9, 15])
a2 = np.array([ 2, 3, 5, 6, 9])

Where a1 is my initial array of elements, and a2 is the array that contains the elements for which I need their indexes in a1.

In this case, the result should be:

a3 = ([4, 5, 1, 2, 8])

This seems like a rather simple operation, but I haven't been able to figure out how to do it.

1
  • Indeed this looks like a duplicate of that question. Sorry I missed it. Commented Jan 9, 2018 at 3:18

1 Answer 1

6

you may try this:

In [378]: (a1[:, None] == a2).argmax(axis=0)
Out[378]: array([4, 5, 1, 2, 8], dtype=int64)
Sign up to request clarification or add additional context in comments.

1 Comment

This solution works, but it a lot slower than np.nonzero(a2[:, None] == a1)[1] given in stackoverflow.com/questions/33678543/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.