[R] How to sum a list of matrices ?

David Winsemius dwinsemius at comcast.net
Wed Mar 10 17:02:13 CET 2010


On Mar 10, 2010, at 10:34 AM, Carlos Petti wrote:

> Dear list,
>
> I have a list of three matrices :
>
> i = list(matrix(1:4,2,2), matrix(3:6,2,2), matrix(9:12,2,2))
>
> I would like to sum the matrices, as follows :
>
> [,1] [,2]
> [1,] 13 19
> [2,] 16 22

 > Reduce("+", i)
      [,1] [,2]
[1,]   13   19
[2,]   16   22


-- 
David.
>
> I used this code :
>
> k <- i[[1]]
> for (j in (2:length(i))) {
> k <- k + i[[j]]}
>
> But, is it possible to sum without a loop ?
>
> Thanks in advance,
> Carlos
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list