# Re: [R] sum of unknown number of matrices

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed, 04 Jun 2008 17:15:44 +0200

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.
>

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. Received on Wed 04 Jun 2008 - 16:38:09 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 Wed 04 Jun 2008 - 17:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.