Computing Euclidean distances between subsequent positions

被刻印的时光 ゝ 提交于 2019-12-01 22:20:51

问题


I have large data frames consisting of pairs of X and Y coordinates, and wish to calculate the Euclidean distances between consecutive coordinates (minimal size is around 2000 pairs of coordinates).

Thus, I want to calculate the distance from row 1 to 2, row 2 to 3, row 3 to 4, etc. This question nicely shows how to calculate the Euclidean distance between the first and last point for track data, but my data are closer to:

dff <- structure(list(A = c(0L, 0L, 0L, 0L, 0L, 0L), T = 0:5, X = c(668L, 670L, 672L, 674L, 676L, 678L), Y = c(259L, 259L, 259L, 259L, 259L, 260L), V = c(NA, 0, 0, 0, 0, 0)), .Names = c("A", "T", "X", "Y", "V"), row.names = c(NA, 6L), class = "data.frame")

It seems like there should be a way to create a loop to do this, but I'm uncertain about how to subscript this. Using dist() is computationally demanding for datasets of this size and in any case I am also unsure of how to extract matrix elements that are one-off from the diagonal.


回答1:


Something like

sqrt(diff(dff$X)^2 + diff(dff$Y)^2)

should work. The key here is the diff function, which gives differences between consecutive items in a vector




回答2:


Another approach, just for fun:

sqrt(apply(apply(dff[,c("X","Y")], 2, diff)^2, 1, sum))


来源:https://stackoverflow.com/questions/18340318/computing-euclidean-distances-between-subsequent-positions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!