pkv
pkv

Reputation: 107

comparison of two numpy arrays

I've two numpy 2d arrays (say A & B, sometime size equal or sometime not equal). I need to compare first column of both arrays and find the index of elements that occur in both arrays.

The below shown code gave me solution whenever the both arrays have different size and all elements of A are not present in B.

C=np.squeeze(A[np.array(np.where(np.in1d(A[:,0],B[:,1]))).T],axis=None)

But it is incorrect whenever all elements of A are present in B.

Can anyone suggest a solution ?

Upvotes: 1

Views: 361

Answers (2)

farhawa
farhawa

Reputation: 10417

import numpy as np

A=np.array([[4, 4, 2, 1, 4, 3, 1, 2],
       [1, 1, 1, 2, 0, 3, 0],
       [4, 3, 1, 1, 2, 1, 1],
       [3, 4, 3, 0, 3, 4, 2],
       [4, 1, 3, 0, 1, 4, 1],
       [1, 1, 1, 2, 2, 2, 0],
       [4, 3, 4, 2, 3, 2, 3],
       [4, 1, 4, 0, 3, 1, 2],
       [3, 2, 3, 2, 4, 4, 4],
       [0, 1, 4, 0, 2, 2, 1]])

B=np.array([[4, 3, 5, 6, 4, 6, 3, 5],
       [6, 3, 4, 4, 4, 6, 5, 4],
       [5, 4, 5, 5, 5, 6, 3, 3],
       [3, 5, 6, 5, 5, 5, 3, 6],
       [5, 6, 5, 3, 5, 5, 5, 3],
       [3, 3, 5, 3, 5, 6, 6, 3],
       [6, 6, 6, 4, 6, 3, 4, 6],
       [4, 4, 3, 5, 6, 6, 3, 3],
       [5, 3, 4, 5, 3, 5, 5, 6],
       [4, 3, 3, 6, 6, 4, 3, 4]])

matched = A.T[0][A.T[0] == B.T[0]]

>> [4,3,4]

Upvotes: 0

Lee
Lee

Reputation: 31100

If A and B are the following:

A=np.random.randint(0,5,(10,8))
B=np.random.randint(3,7,(10,8))
>>> A
array([[4, 4, 2, 1, 4, 3, 1, 2],
       [1, 1, 1, 2, 0, 3, 0, 4],
       [4, 3, 1, 1, 2, 1, 1, 3],
       [3, 4, 3, 0, 3, 4, 2, 0],
       [4, 1, 3, 0, 1, 4, 1, 2],
       [1, 1, 1, 2, 2, 2, 0, 2],
       [4, 3, 4, 2, 3, 2, 3, 2],
       [4, 1, 4, 0, 3, 1, 2, 3],
       [3, 2, 3, 2, 4, 4, 4, 2],
       [0, 1, 4, 0, 2, 2, 1, 4]])
>>> B
array([[4, 3, 5, 6, 4, 6, 3, 5],
       [6, 3, 4, 4, 4, 6, 5, 4],
       [5, 4, 5, 5, 5, 6, 3, 3],
       [3, 5, 6, 5, 5, 5, 3, 6],
       [5, 6, 5, 3, 5, 5, 5, 3],
       [3, 3, 5, 3, 5, 6, 6, 3],
       [6, 6, 6, 4, 6, 3, 4, 6],
       [4, 4, 3, 5, 6, 6, 3, 3],
       [5, 3, 4, 5, 3, 5, 5, 6],
       [4, 3, 3, 6, 6, 4, 3, 4]])

You could use intersect1d to find the values that are in both

np.intersect1d(A,B)
array([3, 4])

And then argwhere to find the indices of the values in, for example, column 0 of A:

[np.argwhere(x==A[:,0]) for x in np.intersect1d(A,B)] 

returns

[array([[3],
   [8]]), array([[0],
   [2],
   [4],
   [6],
   [7]])]

Upvotes: 1

Related Questions