Skip to content
Prev 135513 / 398498 Next

fastest way to compute the squared Euclidean distance between two vectors in R

Jason Liao <jg_liao at yahoo.com> writes:
You might try:

  distance2 <- function(x1, x2) crossprod(x1-x2)

And if you do not have to pass the distance2 function itself to some
other function, you might also spare the indirection trhough the
distance2 function and call crossprod directly (replacing the comma by
a minus sign :-).