Fast weighted euclidean distance between points in arrays

后端 未结 4 1000
清歌不尽
清歌不尽 2020-12-19 14:54

I need to efficiently calculate the euclidean weighted distances for every x,y point in a given array to every other x,y point in another

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 15:02

    You can avoid looping by using code that looks like the following:

    def compute_distances(A, B, W):
        Ax = A[:,0].reshape(1, A.shape[0])
        Bx = B[:,0].reshape(A.shape[0], 1)
        dx = Bx-Ax
    
        # Same for dy
        dist = np.sqrt(dx**2 + dy**2) * W
        return dist
    

    That will run a lot faster in python that anything that loops as long as you have enough memory for the arrays.

提交回复
热议问题