简体   繁体   English

如何计算由包含x,y的矩阵定义的两点之间的欧几里得距离?

[英]How to calculate Euclidian distance between two points defined by matrix containing x, y?

I am very lost in Euclidean distance calculation . 我在欧氏距离计算中非常迷失。 I have found functions dist2{SpatialTools} or rdist{fields} to do this, but they doesn´t work as expected. 我发现函数dist2 {SpatialTools}或rdist {fields}来执行此操作,但它们不能按预期工作。

I suppose that one point has two coordinates in carthesian system, so [x,y]. 我想一个点在carthesian系统中有两个坐标,所以[x,y]。 To measure distance between 2 points (defined by row), I need 4 coordinates for 2 points, so point A: [x1,y1] point B: [x2,y2] 要测量2个点之间的距离(由行定义),我需要2个点的4个坐标,所以点A:[x1,y1]点B:[x2,y2]

Points coordinations: 积分协调:

积分位置

A[0,1]
B[0,0] 
C[1,1]
D[1,1]

I have two matrices: x1 (A and C are there, defined by rows) and x2 (contain B and D). 我有两个矩阵: x1 (A和C在那里,由行定义)和x2 (包含B和D)。 Written in matrix: 写在矩阵中:

library("SpatialTools")
x1<-matrix(c(0,1,1,1), nrow = 2, ncol=2, byrow=TRUE)
x2<-matrix(c(0,0,1,1), nrow = 2, ncol=2, byrow=TRUE)

so I obtain 所以我得到了

> x1
     [,1] [,2]
[1,]    0    1    #(as xy coordinates of A point)
[2,]    1    1    #(same for C point)

> x2
     [,1] [,2]
[1,]    0    0    #(same for B point)
[2,]    1    1    #(same for D point)

To calculate euclidean distance between 计算之间的欧氏距离

A <-> B  # same as x1[1,] <-> x2[1,]
C <-> D  # same as x1[2,] <-> x2[2,]

I assume to obtain EuclidDist : 我假设获得EuclidDist

> x1                           x2                         EuclidDist
     [,1] [,2]                      [,1] [,2]
[1,]    0    1    #A         [1,]    0    0    #B             1
[2,]    1    1    #B         [2,]    1    1    #D             0

I would like just to obtain vector of distances between two points identified by [x,y] coordinates , however, using dist2 I obtain a matrix: 我想获得由[x,y]坐标识别的两点之间的距离矢量 ,但是,使用dist2我得到一个矩阵:

> dist2(x1,x2)
         [,1] [,2]
[1,] 1.000000    1
[2,] 1.414214    0

My question is, which numbers describe the real Euclidean distance between AB and CD from this matrix? 我的问题是,哪些数字描述了这个矩阵中AB和CD之间真正的欧几里德距离? Am I misunderstanding something? 我误会了什么吗? Thank you very much for every advice or any explanation. 非常感谢你的每一条建议或任何解释。

If you just want a vector, something like this will work for you. 如果你只想要一个矢量,这样的东西对你有用。

Try something like this: 尝试这样的事情:

euc.dist <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2))

library(foreach)
foreach(i = 1:nrow(x1), .combine = c ) %do% euc.dist(x1[i,],x2[i,])

This will work for any dimensions. 这适用于任何尺寸。

If you don't want to use foreach, you can use a simple loop: 如果您不想使用foreach,可以使用简单的循环:

dist <- NULL
for(i in 1:nrow(x1)) dist[i] <- euc.dist(x1[i,],x2[i,])
dist

Although, I would recommend foreach (because it's very easy to for various tasks like this). 虽然,我会推荐foreach(因为这对于像这样的各种任务很容易)。 Read more about it in the documentation of the package. 在包的文档中阅读更多相关信息。

The diagonal is what you're looking for. 对角线是你正在寻找的。 The output matrix of dist2 shows the distance between all points. dist2的输出矩阵显示了所有点之间的距离。 The row number in the output corresponds to the row in the first input, and column of the output corresponds to the row in the second input. 输出中的行号对应于第一个输入中的行,输出列对应于第二个输入中的行。 Here's a diagram, hope it makes sense (this is the kind of thing I wish Stack Overflow supported MathJax for): 这是一个图表,希望它有意义(这是我希望Stack Overflow支持MathJax的那种东西):

dist2( A_x A_y     C_x C_y      ( AC  AD
       B_x B_y  ,  D_x D_y )  =   BC  BD ) 

dist2(   x1     ,     x2   )  =   result

In your case, you want the distance from the first point of x1 to the first point of x2 , then the second point of x1 to the second point of x2 , hence the diagonal. 在你的情况下,你想要从第一个点x1到第一个点x2的距离,然后是第二个点x1到第二个点x2 ,因此是对角线。

If you have a lot of data, and you only care about the corresponding pairs, you'll be much better off calculating this directly: 如果您有大量数据,并且只关心相应的对,那么直接计算它会更好:

> x1 <- matrix(c(0, 1, 1, 1), ncol = 2, byrow = T)
> x2 <- matrix(c(0, 0, 1, 1), ncol = 2, byrow = T)
> sqrt(rowSums((x1 - x2)^2))
[1] 1 0

If you've got a whole lot of data (millions of points), it might be worth using foreach like @Shambho suggests. 如果你有大量的数据(数百万点),那么像@Shambho建议的那样使用foreach可能是值得的。

library(rgdal)

library(sp)

##**COORDINATES** DATAFRAME THAT CONTENT THE LATITUDE (LAT) AND LONGITUDE 
##(LON) IN THE COORDINATE REFERENT SYSTEM (CRS) WGS84.

coordinates(COORDINATES) <- ~ LON + LAT

proj4string(COORDINATES) <- CRS("+proj=longlat +datum=WGS84") #ASSIGN THE CRS

Zone <- input$Zone   #UTM ZONE FOR YOUR COUNTRY

COORDINATES <- spTransform(COORDINATES, CRS(paste("+proj=utm", " +zone=", 
                           Zone, " +ellps=WGS84", " +datum=WGS84", " 
                           +units=m", sep="")))  #REPROJECT THE CRS
COORDINATES <- as.data.frame(COORDINATES)
X <- COORDINATES$LON  #EXTRACT THE LOGITUDE VECTOR
Y <- COORDINATES$LAT  #EXTRACT THE LATITUDE VECTOR
MX1 <- X %*% t(X) #CREATE A MATRIX FOR LONGITUDE VECTOR
MX2 <- matrix(rep(t(X),nrow(COORDINATES)), ncol = nrow(COORDINATES), 
              nrow = nrow(COORDINATES)) #CREATE A MATRIX FOR REPEAT LONGITUDE VECTOR
MX <- MX1/MX2 #DEFENITIVE MATRIX FOR LONGITUDE VECTORS
MX <- abs((MX-MX2)**2) #SQUARE SUM OF LONGITUDE VECTORS
colnames(MX)<- paste(COORDINATES$STATION) #ASSIGN COLNAMES
rownames(MX)<- paste(COORDINATES$STATION) #ASSIGN ROWNAMES
MY1 <- Y %*% t(Y) #CREATE A MATRIX FOR LATITUDE VECTOR
MY2 <- matrix(rep(t(Y), nrow(COORDINATES)), ncol = nrow(COORDINATES), 
              nrow = nrow(COORDINATES)) #CREATE A MATRIX FOR REPEAT LATITUDE VECTOR
MY <- MY1/MY2 #DEFENITIVE MATRIX FOR LATITUDE VECTORS
MY <- abs((MY-MY2)*2) #SQUARE SUM OF LONGITUDE VECTORS
colnames(MY)<- paste(COORDINATES$STATION) #ASSIGN COLNAMES
rownames(MY)<- paste(COORDINATES$STATION) #ASSIGN ROWNAMES
EUCLIDEAND <- round((sqrt(MX+MY)/1000), digits = 0) #EUCLIDEAN DISTANCE FOR THESE COORDINATES
EUCLIDEAND <- as.data.frame(EUCLIDEAND)

您可以随时应用真正的等式(为sqldf包编写,但可以轻松转换):

sum(SQRT(power(a.LONG-b.lon,2)+power(a.LAT-b.lat,2))) AS DISTANCE

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

相关问题 如何计算存储在两个单独矩阵的行中的两个点之间的欧几里得距离? - How to calculate Euclidian distance between two points stored in rows of two separate matrixes? R-如何计算两组坐标点之间的距离? - R - How to calculate distance between two sets of coordinate points? 如何计算R中沿线的两个点之间的地理距离? - How to calculate geographic distance between two points along a line in R? 如何计算数据框中两个连续点之间的距离和角度? - How to calculate distance and angle between two consecutive points in data frame? 如何找到序列矩阵中两点之间的距离? - How do I find the distance between two points in a matrix of sequences? 如何计算矩阵中两个整数之间的距离 - R? - How to calculate distance between two integers in a matrix - R? 如何计算矩阵中两个元素之间的最大欧几里得距离 - R? - How to calculate the max euclidean distance between two elements in a matrix - R? 如何使用 R 计算两组点(坐标)之间的欧氏距离的房屋距离 - How to calculate House distance with euclidean distance between two set of points (coordinates) with R 如何计算地理位置和边界之间的距离? - How to calculate distance between geographic points and boundaries? 如何计算数组和矩阵之间的距离 - How to calculate the distance between an array and a matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM