numpynumpy-indexing

Boolean indexing with 2D arrays


I have two arrays, a and b, one 2D and one 1D, containing values of two related quantities that are filled in the same order, such that a[0] is related to b[0] and so on.

I would like to access the element of b where a is equal to a given value, where the value is a 1D array itself.

For example

a=np.array([[0,0],[0,1],[1,0],[1,1]])
b=np.array([0, 7, 9, 4])

value = np.array([0,1])

In 1D cases I could use boolean indexing easily and do

b[a==value]

The result I want is 7.

But in this case, it does not work because it checks each element of b in the comparison, instead of checking subarrays...

Is there a quick way to do this?


Solution

  • The question doesn't seem to match the example, but this returns [7]:

    b[(a == value).all(axis=-1)]