简体   繁体   中英

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):

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

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 .

I changed your second line to loop through the sequence 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

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

This can be translated to R, eg, as follows:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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