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

From: Jason Liao <jg_liao_at_yahoo.com>
Date: Wed, 30 Jan 2008 18:28:26 -0800 (PST)


I have a program which needs to compute squared Euclidean distance between two vectors million of times, which the Rprof shows is the bottleneck. I wondered if there is any faster way than my own simple function

distance2 = function(x1, x2)
{

   temp = x1-x2
   sum(temp*temp)
}

I have searched the R-help archives and can not find anything except when the arguments are matrices. Thanks for any lead.

Jason

Jason Liao, http://www.geocities.com/jg_liao Associate Professor of Biostatistics
Drexel University School of Public Health 1505 Race Street, Mail Stop 1033
Bellet Building, 6th Floor
Philadelphia, PA 19102
phone 215-762-3934



Looking for last minute shopping deals?

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Jan 2008 - 09:02:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Feb 2008 - 15:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive