I have N points in 2D space (N row, 2 column) and I want to find nearest k points for each point (x_n,y_n)
in the point set and then sort them. Here is the codes for this purpose. I want to speed up the following codes:
def nearst_sort(x,y,k):
N = len(x)
A = np.zeros((N,k))
R = np.zeros((N,N))
R = (x - x[np.newaxis].transpose())**2 + (y -y[np.newaxis].transpose())**2
ix = np.argsort(R, kind='stable')
ix = ix.transpose()
A=ix[0:k,:].transpose()
return A
My sample data is as follows:
x y
0 0
0 0.5
0 1
0.5 0
0.5 0.5
0.5 1
1 0
1 0.5
1 1
I have also tried functions from scipy.spatial.KDTree but could not get good results. Any help would be appreciated.