# [R] Vectorizing for weighted distance

R. Michael Weylandt michael.weylandt at gmail.com
Thu Nov 17 23:15:44 CET 2011

```I fail to see why you would need another idea: you asked how to
multiply matrices efficiently, I told you how to multiply matrices
efficiently.

if you want to calculate X1-X2 times W times X1-X2, then simply do so:

X1 <- matrix(1:6, 3)
X2 <- matrix(7:12, 3)
W = matrix(runif(9), 3)

t(X1-X2) %*% W %*% (X1-X2)

which gives

142.7789 142.7789
142.7789 142.7789

You could squeeze out one iota more of speed with

crossprod(X1-X2, W) %*% (X1-X2)

to get the same result, but unless you are doing massive scale linear
processing, I'm not sure it's worth the loss of clarity.

I was only giving you a heads up on the sometimes confusing difference
between matrix multiplication in MATLAB and in R by which a vector is
not a 1d matrix and so does not require explicit transposition.

Michael

On Thu, Nov 17, 2011 at 4:35 PM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
> I'm not quite sure of what you mean by not worry if it's 1d R matrices. X1
> and X2 are both n by d matrices and W is d by d.
>
> Thanks for the help though. Any other ideas?
>
> Thanks
> Sachin
>
> On Friday, November 18, 2011, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>> The fastest is probably to just implement the matrix calculation
>> directly in R with the %*% operator.
>>
>> (X1-X2) %*% W %*% (X1-X2)
>>
>> You don't need to worry about the transposing if you are passing R
>> vectors X1,X2. If they are 1-d matrices, you might need to.
>>
>> Michael
>>
>> On Thu, Nov 17, 2011 at 1:30 AM, Sachinthaka Abeywardana
>> <sachin.abeywardana at gmail.com> wrote:
>>> Hi All,
>>>
>>> I am trying to convert the following piece of matlab code to R:
>>>
>>> XX1 = sum(w(:,ones(1,N1)).*X1.*X1,1);          #square the elements of
>>> X1,
>>> weight it and repeat this vector N1 times
>>> XX2 = sum(w(:,ones(1,N2)).*X2.*X2,1);          #square the elements of
>>> X2,
>>> weigh and repeat this vector N2 times
>>> X1X2 = (w(:,ones(1,N1)).*X1)'*X2;                 #get the weighted
>>> 'covariance' term
>>> XX1T = XX1';                                              #transpose
>>> z = XX1T(:,ones(1,N2)) + XX2(ones(1,N1),:) - 2*X1X2;            #get the
>>> squared weighted distance
>>>
>>> which is basically doing: z=(X1-X2)' W (X1-X2)
>>>
>>> What would the best way (for SPEED) to do this? or is vectorizing as
>>> above
>>> the best? Any hints, suggestions?
>>>
>>> Thanks,
>>> Sachin
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help