Skip to content

Efficient way to Calculate the squared distances for a set of vectors to a fixed vector

4 messages · Wei Wu, Daniel Malter, Enrico Schumann +1 more

#
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.
#
Let's say your fixed vector is x, and y is the list of vectors that you want
to create the squared distance to x with, then:

x<-c(1:5)
y<-list()
y[[1]]<-sample(c(1:5),5)
y[[2]]<-sample(c(1:5),5)
y[[3]]<-sample(c(1:5),5)
y

distances<-lapply(y,function(a,b) crossprod(a-b), b=x) 

#lapply goes over the list elements of y and executes the function for each
of these list elements
#the current list element of "y" is plugged in for "a" and "x" is plugged in
for "b" and the crossproduct is formed

distances<-unlist(distances)

HTH,
Daniel
Wei Wu wrote:
--
View this message in context: http://r.789695.n4.nabble.com/Efficient-way-to-Calculate-the-squared-distances-for-a-set-of-vectors-to-a-fixed-vector-tp3764589p3764616.html
Sent from the R help mailing list archive at Nabble.com.
#
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
#
Hi Wei Wu,

What about:

x <- matrix(rnorm(20000*5),ncol=5)
y <- rnorm(5)
distances <- rowSums((x-y)**2)

Cheers,

Tsjerk

On Wed, Aug 24, 2011 at 8:43 AM, Enrico Schumann
<enricoschumann at yahoo.de> wrote: