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

2011-08-24 Thread Enrico Schumann
You could do something like this: # data nrows - 2L 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

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

2011-08-24 Thread Tsjerk Wassenaar
Hi Wei Wu, What about: x - matrix(rnorm(2*5),ncol=5) y - rnorm(5) distances - rowSums((x-y)**2) Cheers, Tsjerk On Wed, Aug 24, 2011 at 8:43 AM, Enrico Schumann enricoschum...@yahoo.de wrote: You could do something like this: # data nrows - 2L ncols - 5L myVec -

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

2011-08-24 Thread Tsjerk Wassenaar
Yes, sorry, so the distance is colSums((t(x)-y)**2) (I knew that) :S Tsjerk On Wed, Aug 24, 2011 at 9:19 AM, Enrico Schumann enricoschum...@yahoo.de wrote: R will subtract the vector columnwise from the matrix (so the vectors need be the columns). x - matrix(0, nrow = 10L, ncol = 5L) y -

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

2011-08-24 Thread Prof Brian Ripley
On Wed, 24 Aug 2011, Tsjerk Wassenaar wrote: Yes, sorry, so the distance is colSums((t(x)-y)**2) (I knew that) :S Did you know that ** is deprecated (and almost undocumented), so your readers can hardly be expected to understand that? Please use the documented operator ^ . Tsjerk On