简体   繁体   English

用R计算欧氏距离

[英]Compute euclidean distance with R

I have two data frames with coordinates and different lengths, I'm trying to compute euclidean distance from each point of first date frame (for example 2 points) to every point of second data frame (for example 4 points): 我有两个坐标和长度不同的数据框,我试图计算从第一个日期帧的每个点(例如2个点)到第二个数据帧的每个点(例如4个点)的欧几里德距离:

Result: 结果:

point[1] dist1, dist2, dist3, dist4

point[2] dist1, dist2, dist3, dist4

This is my script: 这是我的脚本:

i=1
for (i in dim(coordinates)[1]) {
  result[i]<- 
  sqrt((coordinates[i,1] - reference[,1])^2 + 
  (coordinates[i,2] - reference[,2])^2 + 
  (coordinates[i,3] - reference[,3])^2)
}        

But it only returns distances from last point (point[2]) How can I fix the scrip??, any help would be great 但它只返回距离最后一点的距离(点[2])如何修复脚本?任何帮助都会很棒

Thanks in advance 提前致谢

I make this mistake all the time. 我总是犯这个错误。 dim returns a single number, so you need to loop i from 1 through dim . dim返回一个数字,因此你需要将i1循环到昏暗

I changed your second line to loop through the sequence 1:dim(coordinates)[1]. 我改变了你的第二行来循环序列1:dim(coordinates)[1]。

i=1
for (i in 1:dim(coordinates)[1]) {
  result<- 
  sqrt((coordinates[i,1] - reference[,1])^2 + 
  (coordinates[i,2] - reference[,2])^2 + 
  (coordinates[i,3] - reference[,3])^2)
} 

Now you just need to make sure your result vector has two slots. 现在您只需要确保结果向量有两个插槽。 If it doesn't work, put this at the beginning. 如果它不起作用,请将其放在开头。

 result <- rep(NA, 1:dim(coordinates)[1])

One can take advantage of matrix algebra and calculate the distances without loops. 人们可以利用矩阵代数并计算没有循环的距离。

For two matrices A, B where the rows represent instances and the columns represent the coordinates (ie, in 3 dimensions both matrices have 3 columns), the matrix of squared distances can be calculated as 对于两个矩阵A,B,其中行表示实例,列表示坐标(即,在3维中,两个矩阵都有3列),平方距离矩阵可以计算为

d^2(A,B) = ||AB||^2 = A^2 + B^2 - 2*A*B d ^ 2(A,B)= || AB || ^ 2 = A ^ 2 + B ^ 2 - 2 * A * B

This can be translated to R, eg, as follows: 这可以转换为R,例如,如下:

d <- sqrt(    matrix(diag(A %*% t(A)), nrow=dim(A)[1], ncol=dim(B)[1])
          + t(matrix(diag(B %*% t(B)), nrow=dim(B)[1], ncol=dim(A)[1]))
          - 2*A %*% t(B) )

The resulting distance matrix has a row for each instance in A and a column for each instance in B. 结果距离矩阵在A中为每个实例都有一行,在B中为每个实例都有一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM