1

I would like to ask a question with numpy array masking.

For instance given the array below:

 a b
 1 2
 3 4
 5 6
 6 5

I have another array which is

 a b
 1 2
 3 4

I want to compare two arrays and find the index numbers of second array in the first array.

For instance, the solution should be index=[0,1]

I have tried with

 np.where np.where(~(np.abs(a - b[:,None]).sum(-1)==0).any(0))

but does not give me the final result

thanks for suggestions!

Remi Cuingnet
  • 943
  • 10
  • 14

2 Answers2

0

A possible solution, based on Broadcasting, where ar1 and ar2 are the two arrays, respectively:

np.nonzero(np.any(np.all(ar1 == ar2[:,None], axis=2), axis=0))[0]

Output:

array([0, 1])
PaulS
  • 21,159
  • 2
  • 9
  • 26
0
a = np.array([[1,2],[3,4],[5,6],[6,5]])
b = np.array([[1,2],[3,4]])

np.where(np.all(a == b[:,None], axis=2))[1] # np.array([0,1])
Jelle Westra
  • 526
  • 3
  • 8