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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Jan 31 10:19:49 CET 2008


hits=0.5 tests=BAYES_00,ETHZ_GEOCITIES,FORGED_RCVD_HELO
X-USF-Spam-Flag: NO

try this:

distance2 <- function (x1, x2) {
   temp <- x1 - x2
   sum(temp * temp)
}

x1 <- rnorm(1e06)
x2 <- rnorm(1e06)

system.time(for (i in 1:100) distance2(x1, x2))
system.time(for (i in 1:100) crossprod(x1 - x2))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Jason Liao" <jg_liao at yahoo.com>
To: <r-help at r-project.org>
Sent: Thursday, January 31, 2008 3:28 AM
Subject: [R] fastest way to compute the squared Euclidean distance 
betweentwo vectors in R


>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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list