[英]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.现在,我想计算第 1 列和第 2 列中的点之间的距离。所有距离都应该从第 0 行计算,即,(x1,y1 = 15,1) 但 x2,y2 是从元素第 1 行变化的变量到第 6 行。 And I want save this distance as a 'list'.我想将此距离保存为“列表”。 Please help me in solving this in python请帮助我在 python 中解决这个问题
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
.毕达哥拉斯定理指出sqrt(a^2+b^2)=c
其中c
是到达点a
和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
:如果您不想导入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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.