Hi R, I have a list of matrices. I need to get the sum of all the matrices in the list. Example: a=b=c=d=matrix(1:4,2,2) l=list(a,b,c,d) I need:> a+b+c+d[,1] [,2] [1,] 4 12 [2,] 8 16 Something like do.call("+",l) is not working...why is this? I may not be knowing the number of matrices in the list... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
try a simple for loop, it will be fast enough in this case, e.g., matSums <- function (lis) { out <- array(data = 0, dim = dim(lis[[1]])) for (i in seq(along = lis)) out <- out + lis[[i]] out } a <- b <- c <- d <- matrix(1:4, 2, 2) l <- list(a, b ,c, d) matSums(l) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, June 04, 2008 4:53 PM Subject: [R] sum of unknown number of matrices> Hi R, > > > > I have a list of matrices. I need to get the sum of all the matrices > in > the list. > > > > Example: > > a=b=c=d=matrix(1:4,2,2) > > l=list(a,b,c,d) > > > > > > I need: > >> a+b+c+d > > [,1] [,2] > > [1,] 4 12 > > [2,] 8 16 > > > > Something like do.call("+",l) is not working...why is this? > > > > > > I may not be knowing the number of matrices in the list... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Shubha Vishwanath Karanth wrote:> I need: > >> a+b+c+d > > [,1] [,2] > > [1,] 4 12 > > [2,] 8 16 > > > > Something like do.call("+",l) is not working...why is this?Because do.call constructs a function call with the elements of l as arguments, so you end up with: "+"(1:4, 1:4, 1:4, 1:4) but "+" only takes two arguments. Use 'Reduce': > Reduce("+",l) [,1] [,2] [1,] 4 12 [2,] 8 16 Barry
Dear Shubha, This problem was coincidentally used as an illustration in the Help Desk column in the current R News. Actually, the brute-force method of using a loop to accumulate the sum works quite well; a more elegant alternative, recently brought to my attention by Kurt Hornik, uses the Reduce() function. Here's an example:> matrices <- vector(mode="list", length=10000) > for (i in 1:10000)+ matrices[[i]] <- matrix(rnorm(10000), 100, 100)> > system.time({+ S <- matrix(0, 100, 100) + for (i in 1:10000) S <- S + matrices[[i]] + }) user system elapsed 0.59 0.00 0.59> > system.time(S1 <- Reduce("+", matrices))user system elapsed 0.60 0.00 0.59> > range(S1 - S)[1] 0 0 I hope this helps, John ------------------------------ John Fox, Professor Department of Sociology McMaster University Hamilton, Ontario, Canada web: socserv.mcmaster.ca/jfox> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]On> Behalf Of Shubha Vishwanath Karanth > Sent: June-04-08 10:54 AM > To: r-help at stat.math.ethz.ch > Subject: [R] sum of unknown number of matrices > > Hi R, > > > > I have a list of matrices. I need to get the sum of all the matrices in > the list. > > > > Example: > > a=b=c=d=matrix(1:4,2,2) > > l=list(a,b,c,d) > > > > > > I need: > > > a+b+c+d > > [,1] [,2] > > [1,] 4 12 > > [2,] 8 16 > > > > Something like do.call("+",l) is not working...why is this? > > > > > > I may not be knowing the number of matrices in the list... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
G'day Shubha, On Wed, 4 Jun 2008 20:23:35 +0530 "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> wrote:> Something like do.call("+",l) is not working...why is this?Well, as the error message says, "+" is either a unary or a binary operator, i.e. it takes either one or two arguments, but not more.> I may not be knowing the number of matrices in the list...This is perhaps a bit complicated but it works: R> a=b=c=d=matrix(1:4,2,2) R> l=list(a,b,c,d) R> library(abind) ## may have to install this package first R> apply(do.call(abind, list(l, along=3)), 1:2, sum) [,1] [,2] [1,] 4 12 [2,] 8 16 HTH. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6515 4416 (secr) Dept of Statistics and Applied Probability +65 6515 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba