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 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]]
Hi, Look at the R News 8/1 in the R Help Desk. Alain On 10-Mar-10 16:34, 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 > > 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. > >-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50
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