[R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector

Enrico Schumann enricoschumann at yahoo.de
Wed Aug 24 08:43:51 CEST 2011


You could do something like this:

# data
nrows <- 20000L
ncols <- 5L
myVec <- array(rnorm(nrows * ncols), dim = c(nrows, ncols))
y <- rnorm(ncols)

temp <- t(myVec) - y
result <- colSums(temp * temp)

# check
all.equal(as.numeric(crossprod(myVec[1L, ] - y)), result[1L])
#...

(And don't use a data.frame, but a matrix.)

regards,
Enrico
> -----Ursprüngliche Nachricht-----
> Von: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] Im Auftrag von Wei Wu
> Gesendet: Mittwoch, 24. August 2011 07:18
> An: r-help at r-project.org
> Betreff: [R] Efficient way to Calculate the squared distances 
> for a set ofvectors to a fixed vector
> 
> I am pretty new to R. So this may be an easy question for most of you.
>  
> I would like to calculate the squared distances of a large 
> set (let's say 20000) of vectors (let's say dimension of 5) 
> to a fixed vector.
>  
> Say I have a data frame MY_VECTORS with 20000 rows and 5 
> columns, and one 5x1 vector y. I would like to efficiently 
> calculate the squared distances between each of the 20000 
> vectors in MY_VECTORS and y.
>  
> The squared distance between two vectors x and y can be calculated:
> distance <- crossprod(x-y)
>  
> Without looping, what is the efficient code to achieve this?
>  
> Thanks.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list