calculate distance of 2 list of points in numpy

前端 未结 3 553
梦毁少年i
梦毁少年i 2020-12-02 01:44

I have 2 lists of points as numpy.ndarray, each row is the coordinate of a point, like:

a = np.array([[1,0,0],[0,1,0],[0,0,1]])
b = np.array([[1,1,0],[0,1,1]         


        
3条回答
  •  余生分开走
    2020-12-02 01:47

    If you have 2 each 1-dimensional arrays, x and y, you can convert the arrays into matrices with repeating columns, transpose, and apply the distance formula. This assumes that x and y are coordinated pairs. The result is a symmetrical distance matrix.

    x = [1, 2, 3]
    y = [4, 5, 6]
    xx = np.repeat(x,3,axis = 0).reshape(3,3)
    yy = np.repeat(y,3,axis = 0).reshape(3,3)
    dist = np.sqrt((xx-xx.T)**2 + (yy-yy.T)**2)
    
    
    dist
    Out[135]: 
    array([[0.        , 1.41421356, 2.82842712],
           [1.41421356, 0.        , 1.41421356],
           [2.82842712, 1.41421356, 0.        ]])
    

提交回复
热议问题