Akhilesh Arkala
Akhilesh Arkala

Reputation: 37

Python: Calculating the distance between points in an array

I would like to calculate the distance between data points in an array.

Example

A1 = array([[ 54.        ,  15.        ,   1.        ],
       [ 55.        ,  13.66311896,   1.        ],
       [ 56.        ,  10.16311896,   1.95491503],
       [ 57.        ,   5.83688104,   4.45491503],     # shape 6rows x 3 col
       [ 58.        ,   2.33688104,   7.54508497],
       [ 59.        ,   1.        ,  10.04508497],
       [ 60.        ,   1.        ,  11.        ],

Now, I want to calculate the distance between points in column 1 & column 2. and all the distance should be calculated from Row 0 ie.,(x1,y1 = 15,1) but x2,y2 is variable changing from elements row 1 to row6. And I want save this distance as a 'list'. Please help me in solving this in python

Upvotes: 0

Views: 445

Answers (1)

Regretful
Regretful

Reputation: 335

Pythagoras theorem states sqrt(a^2+b^2)=c where c is the distance between the tips of the orthogonal lines reaching point a and b.

import math
from math import sqrt
dist_list=[]
for i in A1[1:]:
    dist=sqrt(pow(A1[0][1]-i[1],2)+pow(A1[0][2]-i[2],2))
    dist_list.append(dist)

If you dont want to import math:

for i in A1[1:]:
    dist=pow(pow(A1[0][1]-i[1],2)+pow(A1[0][2]-i[2],2),0.5)
    dist_list2.append(dist)

Upvotes: 1

Related Questions