3

How can I find the indexes of the rows that match exactly between two numpy arrays. For example:

x = np.array(([0,1],
              [1,0],
              [0,0]))
y = np.array(([0,1],
              [1,1],
              [0,0]))

This should return:

matches = [0,2] # Match at row no 0 and 2
kPow989
  • 426
  • 5
  • 22

2 Answers2

5
np.flatnonzero((x == y).all(1))
# array([0, 2])

or:

np.nonzero((x == y).all(1))[0]

or:

np.where((x == y).all(1))[0]
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

This works for every pair of numpy arrays if the length is the same:

matches = [i for i in range(len(x)) if x[i].tolist()==y[i].tolist()]