Skip to content
Prev 139499 / 398506 Next

Distances between two datasets of x and y co-ordinates

From: adrian at maths.uwa.edu.au
The margin may not be as large as you imagine:

crossdist2 <- function(x1, y1, x2, y2) {
    c1 <- complex(real=x1, imaginary=y1)
    c2 <- complex(real=x2, imaginary=y2)
    dist <- outer(c1, c2, function(z1, z2) Mod(z1-z2))
    dist
}

R> set.seed(17)
R> n <- 1000
R> x1 <- rnorm(n)
R> y1 <- rnorm(n)
R> x2 <- rnorm(n)
R> y2 <- rnorm(n)
R> system.time(d2 <- crossdist2(x1, y1, x2, y2))
   user  system elapsed 
   1.03    0.09    1.17 
R> library(spatstat)
R> system.time(d1 <- crossdist2(x1, y1, x2, y2))
   user  system elapsed 
   1.08    0.03    1.12 
R> all.equal(d1, d2)
[1] TRUE

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachme...{{dropped:15}}