I have a point and an array of values. I wish to find the closest point y in the array such that x != y. Currently I am using the standard Scipy KDTree approach in Python, but this does not consider only unique points. Is there any workaround to this or does it require a manual implementation? Thanks!
2D ARRAY SOLUTION: I recycled most of the same method as before but this time it ought to work for your structure
import math
X = [0,0] # Your cooridinate
x1 = X[0]
y1= X[1]
array = [[1,1],[0,1],[1,0],[-2,2]] # Sample array of data
smallestDistance = 9999 # Make it big so it can be replaced immediately, essentially just a placeholder
for point in array:
x2 = point[0]
y2 = point[1]
separation = math.hypot(x2 - x1, y2 - y1) #Distance equation in easy format
if separation < smallestDistance and separation!=0: # Could make this <= instead of < if you want to replace any ties for closest point
smallestDistance = separation
closestPoint = point
print(f"Nearest cooridinate = {closestPoint}, separation from {X} is: {smallestDistance}")
OLD SOLUTION FOR 1D ARRAY: I'm not too sure what the structure of your array is, but from what I can tell you've got a 1d array and a given value within in; you want the closest unique value in the array to the original one in question. If this is correct, the quick and dirty solution could be something akin to:
smallestDifference = 99999999 #Make this some large number
y = array[someindex]
for thing in array:
difference = abs(thing-y)
if difference<smallestDifference & difference != 0: #Must be closer than the last recorded closest point, AND non-zero i.e. a unique point
smallestDifference = difference #New smallest distance between points
closestPoint = thing #Record the new closest point
print(f"x = {closestPoint}, |y-x| = {smallestDifference}")
Might be a bit trickier if this a >1d array and probably not the fastest solution since it has to check every point .. but probably would get the job done :-)