Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of two numpy arrays of different dimensions by column

I have two different numpy arrays given. First one is two-dimensional array which looks like (first ten points):

[[  0.           0.        ]
 [ 12.54901961  18.03921569]
 [ 13.7254902   17.64705882]
 [ 14.11764706  17.25490196]
 [ 14.90196078  17.25490196]
 [ 14.50980392  17.64705882]
 [ 14.11764706  17.64705882]
 [ 14.50980392  17.25490196]
 [ 17.64705882  18.03921569]
 [ 21.17647059  34.11764706]]

the second array is just one-dimensional which looks like (first ten points):

[ 18.03921569  17.64705882  17.25490196  17.25490196  17.64705882
  17.64705882  17.25490196  17.64705882  21.17647059  22.35294118]

Values from the second (one-dimension) array could occur in first (two-dimension) one in the first column. F.e. 17.64705882

I want to get an array from the two-dimension one where values of the first column match values in the second (one-dimension) array. How to do that?

like image 718
x4k3p Avatar asked Jan 22 '16 00:01

x4k3p


1 Answers

You can use np.in1d(array1, array2) to search in array1 each value of array2. In your case you just have to take the first column of the first array:

mask = np.in1d(a[:, 0], b)
#array([False, False, False, False, False, False, False, False,  True,  True], dtype=bool)

You can use this mask to obtain the encountered values:

a[:, 0][mask]
#array([ 17.64705882,  21.17647059])
like image 110
Saullo G. P. Castro Avatar answered Oct 06 '22 01:10

Saullo G. P. Castro