17 Jul

Fast Euclidean Distance Function

Holà GISpo,

Today, a simple function to (highly) speed up the standard Euclidean distance function in R when you use large matrix (n> 100.000.000).

euc_dist <- function(m) {mtm <- Matrix::tcrossprod(m); sq <- rowSums(m*m);  sqrt(outer(sq,sq,"+") - 2*mtm)} 
 
# Example
M <- matrix(rexp(1000000, rate=.1), ncol=1000)
 
ptm <- proc.time()
d <- dist(M, method = "euclidean")
proc.time() - ptm
 
#   user  system elapsed 
#   1.08    0.00    1.08 
 
ptm <- proc.time()
d2 <- euc_dist(M)
d2 <- as.dist(d2)
proc.time() - ptm
 
#   user  system elapsed 
#  0.424   0.008   0.429 
 
isTRUE(all.equal(as.matrix(d), as.matrix(d2)))
#TRUE

One thought on “Fast Euclidean Distance Function

Leave a Reply

Your email address will not be published. Required fields are marked *