I have a one-dimensional grid that is defined as a list of sorted floating point values. The points are not equidistant, but it is guaranteed that there is no any pair of clashing points (distance==0).
I need to find the most efficient way to snap any given value to the closest grid point. The most clever way I could think of was as follows (np is numpy and myGrid is a numpy array)
absDiff = np.abs(myGrid - myValue)
ix = np.argmax(absDiff)
snappedValue = myGrid[ix]
The problem is that this approach is too slow and I need a more efficient one.
1 Answer