Note that there are some explanatory texts on larger screens.

plurals
  1. PONearest neighbor 1 dimensional data with a specified range
    text
    copied!<p>I have two nested lists A and B:</p> <p><code>A = [[50,140],[51,180],[54,500],......]</code></p> <p><code>B = [[50.1, 170], [51,200],[55,510].....]</code></p> <p>The 1st element in each inner list runs from 0 to around 1e5, the 0th element runs from around 50 up to around 700, these elements are unsorted. What i want to do, is run through each element in A[n][1] and find the closest element in B[n][1], but when searching for the nearest neighbor i want to only search within an interval defined by A[n][0] plus or minus 0.5.</p> <p>I have been using the function:</p> <pre><code>def find_nearest_vector(array, value): idx = np.array([np.linalg.norm(x+y) for (x,y) in array-value]).argmin() return array[idx] </code></pre> <p>Which finds the nearest neighbor between the coordinates <code>A[0][:]</code>and <code>B[0][:]</code>, for example. However, this I need to confine the search range to a rectangle around some small shift in the value A[0][0]. Also, I do not want to reuse elements - I want a unique bijection between each value A[n][1] to B[n][1] within the interval A[n][0] +/- 0.5.</p> <p>I have been trying to use Scipy's KDTree, but this reuses elements and I don't know how to confine the search range. Effectively, I want to do a one dimensional NNN search on a two dimensional nested list along a specific axis where the neighborhood in which the NNN search is within a hyper-rectangle defined by the 0th element in each inner list plus or minus some small shift.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload