Reputation: 11
I have 2 arrays, A and B with sizes (2, 25919) and (2, 64788) respectively. I have been attempting to compare the row and column values of array B against the values in point A to see which ones fall within a specified radius of point A.
I have tried the sectioning and KDTree examples in Given two lists of 2d points, how to find the closest point in the 2nd list for every point in the 1st list? , which is a close question to my own. However, since my own arrays are 2 different sizes, I get errors.
My own code right now looks like this:
A = [decamra, decamdec]
B = [ra, dec]
tree = KDTree(A)
results = tree.query_ball_point(B, r=r)
result = sum(len(list) for lis in results)
which returns the error "x must consist of vectors of length 25919 but has shape (2, 64788)".
Again, I want the code to find all the points in B that fall within the radius of a point of A and I have tried so many other different methods but the array size seems to be the biggest issue I am having here. Is there a way to modify this code so the array sizes don't have to match?
Upvotes: 0
Views: 120
Reputation: 3609
Take an outer subtraction using broadcasting to calculate every possible pair distance and then compare. Since you need all of A, you can section B and just concatenate the results.
A = np.random.randint(0, 100, (2, 25919))
B = np.random.randint(0, 100, (2, 10000)) # B too large
radius = 5
dist = A[:, :, None] - B[:, None]
dist = np.square(dist).sum(axis=0)
dist = np.sqrt(dist)
B_close = B[:, np.any(dist <= radius, axis=0)]
Upvotes: 0