Michel Keijzers
Michel Keijzers

Reputation: 15357

Get numpy array with only certain indices

Is there a numpy function (or algorithm) that only returns the items of an array depending on and and-function of the values of two other boolean arrays.

E.g.

>>> b1 = numpy.array([False, False, True, True , True])
>>> b2 = numpy.array([True , False, True, False, True])
>>> v  = numpy.array([2    , 4    , 6   , 8,     10  ])

Then the function should return:

numpy.array([6, 10])

Because 6 and 10 are the values where both the corresponding b1 and b2 values are True.

Edited according accepted answer below:

>>> v[b1 & b2]
array([ 6, 10])

Upvotes: 2

Views: 1360

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601519

v[b1 & b2]

will do the trick.

Upvotes: 4

Related Questions