Re: [R] Speed up sum of outer products?

From: AjayT <>
Date: Tue, 01 Mar 2011 10:52:57 -0800 (PST)

Hey thanks alot guys !!! That really speeds things up !!! I didn't know %*% and crossprod, could operate on matrices. I think you've saved me hours in calculation time. Thanks again.

> system.time({C=matrix(0,50,50);for(i in 1:n)C = C + (X[i,] %o% X[i,])})

   user system elapsed
   0.45 0.00 0.90
> system.time({C1 = t(X) %*% X})

   user system elapsed
   0.02 0.00 0.05
> system.time({C2 = crossprod(X)})

   user system elapsed
   0.02 0.00 0.02

View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 01 Mar 2011 - 18:58:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Mar 2011 - 22:00:17 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive