Willem
Willem

Reputation: 1

Use the positions of the values of an array in another array with numpy

I want to get hold of the positions in an array and want to extract the values in those positions from another array. I have two arrays:

Array_1 = (1, 0, 23, 4, 0, 0, 17, 81, 0, 10)
Array_2 = (11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

With

a, b = numpy.unique(Array_1)

I get the following: a contains the values and b the positions.

a = (1, 23, 4, 17, 81, 10)
b = (0, 2, 3, 6, 7, 9)

I want the values of Array_2 at the positions of Array_1. In other words, I want:

c = (11, 13, 14, 17, 18, 20)

How do I get hold of those values?

Upvotes: 0

Views: 160

Answers (1)

user2379410
user2379410

Reputation:

Numpy supports vectorized indexing, see "Integer array indexing". In practice, this means you can do:

a, b = numpy.unique(Array_1, return_index=True)
c = Array_2[b]

Upvotes: 3

Related Questions