I need to sort a numpy array of points by increasing distance from another point.
import numpy as np
def dist(i,j,ip,jp):
return np.sqrt((i-ip)**2+(j-jp)**2)
arr = np.array([[0,0],[1,2],[4,1]])
What I would like to do is use function dist(1,1,ip,jp) between a fixed point [i,j]=[1,1] and each ordered pair [ip,jp] in arr to return arr with each element sorted from lowest to highest proximity to [i,j]. Anyone have a quick solution for this?
The output I want is new_arr = np.array([[1,2],[0,0],[4,1]])
I have some ideas but they're wildly inefficient seeming.
Thanks!
np.array(sorted(arr, key=lambda x: dist(1,1,x[0], x[1])))
. – Phalanstery