Isn't the following the canonical (R-ish) way of doing this: X = matrix(rnorm(1000*50),1000,50) system.time({C1 = t(X) %*% X}) # Phil's example
C2 <- crossprod(X) # use crossprod instead > all.equal(C1,C2) [1] TRUE > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Phil Spector > Sent: Tuesday, March 01, 2011 12:31 PM > To: AjayT > Cc: r-help@r-project.org > Subject: Re: [R] Speed up sum of outer products? > > What you're doing is breaking up the calculation of X'X > into n steps. I'm not sure what you mean by "very slow": > > > X = matrix(rnorm(1000*50),1000,50) > > n = 1000 > > system.time({C=matrix(0,50,50);for(i in 1:n)C = C + (X[i,] %o% X[i,])}) > user system elapsed > 0.096 0.008 0.104 > > Of course, you could just do the calculation directly: > > > system.time({C1 = t(X) %*% X}) > user system elapsed > 0.008 0.000 0.007 > > all.equal(C,C1) > [1] TRUE > > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spec...@stat.berkeley.edu > > > > On Tue, 1 Mar 2011, AjayT wrote: > > > Hi, I'm new to R and stats, and I'm trying to speed up the following sum, > > > > for (i in 1:n){ > > C = C + (X[i,] %o% X[i,]) # the sum of outer products - this is very > slow > > according to Rprof() > > } > > > > where X is a data matrix (nrows=1000 X ncols=50), and n=1000. The sum has to > > be calculated over 10,000 times for different X. > > > > I think it is similar to estimating a co-variance matrix for demeaned data > > X. I tried using cov, but got different answers, and it was'nt much quicker? > > > > Any help gratefully appreciated, > > > > -- > > View this message in context: http://r.789695.n4.nabble.com/Speed-up-sum-of- > outer-products-tp3330160p3330160.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help@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@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@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.