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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 24 11:10:57 CEST 2011


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 Wed, Aug 24, 2011 at 9:19 AM, Enrico Schumann
> <enricoschumann at 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 <- 1:5
>> x - y
>>
>>      [,1] [,2] [,3] [,4] [,5]
>>  [1,]   -1   -1   -1   -1   -1
>>  [2,]   -2   -2   -2   -2   -2
>>  [3,]   -3   -3   -3   -3   -3
>>  [4,]   -4   -4   -4   -4   -4
>>  [5,]   -5   -5   -5   -5   -5
>>  [6,]   -1   -1   -1   -1   -1
>>  [7,]   -2   -2   -2   -2   -2
>>  [8,]   -3   -3   -3   -3   -3
>>  [9,]   -4   -4   -4   -4   -4
>> [10,]   -5   -5   -5   -5   -5
>>
>>
>>
>>
>>> -----Ursprüngliche Nachricht-----
>>> Von: Tsjerk Wassenaar [mailto:tsjerkw at gmail.com]
>>> Gesendet: Mittwoch, 24. August 2011 09:02
>>> An: Enrico Schumann
>>> Cc: Wei Wu; r-help at r-project.org
>>> Betreff: Re: [R] Efficient way to Calculate the squared
>>> distances for a set ofvectors to a fixed vector
>>>
>>> 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:
>>>>
>>>> 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.
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>>
>>>
>>> --
>>> Tsjerk A. Wassenaar, Ph.D.
>>>
>>> post-doctoral researcher
>>> Molecular Dynamics Group
>>> * Groningen Institute for Biomolecular Research and Biotechnology
>>> * Zernike Institute for Advanced Materials University of
>>> Groningen The Netherlands
>>
>>
>
>
>
> -- 
> Tsjerk A. Wassenaar, Ph.D.
>
> post-doctoral researcher
> Molecular Dynamics Group
> * Groningen Institute for Biomolecular Research and Biotechnology
> * Zernike Institute for Advanced Materials
> University of Groningen
> The Netherlands
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list