# [SciPy-User] Finding closest point in array - inverse of KDTree

Ant antoniy.py at gmail.com
Thu Oct 12 09:21:12 EDT 2017

```Sorry, I included your reply into Robert. I am a little confused by this system :D

On 11 Oct 2017, 22:15 +0200, Daπid <davidmenhur at gmail.com>, wrote:
> > On 11 October 2017 at 19:01, Antonio Polino <antoniy.py at gmail.com> wrote:
> > > On the other hand, the array A is always the same, only k changes. So it would be beneficial to use some auxiliary structure (like a "inverse KDTree") on A, and then query the results on the small array k.
> >
> > You can sort A and use searchsorted to find the positions of the midpoints of k. Then, you assign the values to the group.
> >
> >
> >     >>> A = np.asarray([3, 4, 5, 6])
> >     >>> k = np.asarray([3, 4.1, 5.2])
> >     >>> midpoints = np.array([ 3.55,  4.65])   #  k[:-1] + np.diff(k) / 2
> >     >>> np.searchsorted(A, midpoints)
> >     array([1, 2])
> >
> > So, this means that A[:1] belong to k[0], A[1:2] belong to k[2] and A[2:] belong to k[3]
> >
> >
> > /David.
> >
> > PS: in your later email I see your name as Ant.
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at python.org
> https://mail.python.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...