Intersection of two numpy arrays of different sizes in columns

I have two different numpy arrays. The first is a two-dimensional array that 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] 

The values โ€‹โ€‹of the second (one-dimensional) array can occur in the first (two-dimensional) order in the first column. FE 17.64705882

I want to get an array from two-dimensional, where the values โ€‹โ€‹of the first column correspond to the values โ€‹โ€‹in the second (one-dimensional) array. How to do it?

+6
source share
1 answer

You can use np.in1d(array1, array2) to search in array1 each value of array2 . In your case, you just need 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 get the values โ€‹โ€‹found:

 a[:, 0][mask] #array([ 17.64705882, 21.17647059]) 
+5
source

All Articles