[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@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.
Re: [R] sum of unknown number of matrices
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 [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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@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 __ 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.
Re: [R] sum of unknown number of matrices
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 __ 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.
Re: [R] sum of unknown number of matrices
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=1) for (i in 1:1) + matrices[[i]] - matrix(rnorm(1), 100, 100) system.time({ + S - matrix(0, 100, 100) + for (i in 1:1) S - S + matrices[[i]] + }) user system elapsed 0.590.000.59 system.time(S1 - Reduce(+, matrices)) user system elapsed 0.600.000.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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Shubha Vishwanath Karanth Sent: June-04-08 10:54 AM To: [EMAIL PROTECTED] 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@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.
Re: [R] sum of unknown number of matrices
Thanks all...Reduce() is the new function I learnt today... Thanks... BR, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com -Original Message- From: Barry Rowlingson [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 04, 2008 8:49 PM To: Shubha Vishwanath Karanth Cc: [EMAIL PROTECTED] Subject: Re: [R] sum of unknown number of matrices 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 This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ 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.
Re: [R] sum of unknown number of matrices
G'day Shubha, On Wed, 4 Jun 2008 20:23:35 +0530 Shubha Vishwanath Karanth [EMAIL PROTECTED] 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 TurlachTel.: +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: [EMAIL PROTECTED] Singapore 117546http://www.stat.nus.edu.sg/~statba __ 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.