How to calculate the Euclidean distance between two points defined by a matrix containing x, y?

I really lost in the Euclidean distance calculation . For this, I found the functions dist2 {SpatialTools} or rdist {fields}, but they do not work properly.

I believe that one point has two coordinates in the map system, therefore [x, y]. To measure the distance between two points (determined by the line), I need 4 coordinates for 2 points, so point A: [x1, y1] point B: [x2, y2]

Point coordinates:

Points position

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

I have two matrices: x1 (there are A and C defined by rows) and x2 (contain B and D). It is written in a 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 get

 > 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 the Euclidean distance between

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

I suppose to get an 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 just like to get the distance vector between two points identified by the coordinates [x, y] , however, using dist2 , I get the matrix:

 > 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? I do not understand something? Thanks so much for every tip or any explanation.

+6
source share
3 answers

If you only need 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 measurements.

If you do not want to use foreach, you can use a simple loop:

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

Although, I would recommend foreach (because it is very easy for various tasks like this). Read more about this in the package documentation.

+10
source

Diagonal is what you are looking for. The output matrix dist2 shows the distance between all points. The row number on the output corresponds to the row in the first input, and the output column corresponds to the row in the second input. Here is a diagram, hope this makes sense (this is what I want Stack to support MathJax for):

 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 x1 to the first point x2 , then the second point x1 to the second point x2 , therefore, the diagonal.

If you have a lot of data, and you only care about the corresponding pairs, it will be much better for you to calculate 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 have a whole series of data (millions of points), it might be worth using foreach , as @Shambho suggests.

0
source

You can always apply the true equation (written for the sqldf package, but it can be easily converted):

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

Source: https://habr.com/ru/post/972211/


All Articles