0

I have two arrays with different size

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

I need

c = np.array([False, True, False, False, False, True, False])

i.e. array 'b' have rows [5, 6] and [2, 4] in array 'a'. Currently, I do this by

logical = np.zeros(a.shape[0]).astype(bool)
for i in range(b.shape[0]):
    logical += np.all(a == b[i], axis=1)

Is there any numpy code for doing this?

vinu
  • 457
  • 4
  • 11

1 Answers1

3

Let's try broadcasting:

(a[None,:] == b[:,None]).all(-1).any(0)

Output:

array([False,  True, False, False, False,  True, False])
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74