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
0

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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()]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.