2

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
  • 416
  • 4
  • 21

2 Answers2

4
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
  • 195,464
  • 25
  • 298
  • 322
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()]