Dear all, I think that my question is very simple but I failed to solve it. I have a list which elements are matrices like this:>mylist[[1]] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [[2]] [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] [,1] [,2] [,3] [1,] 8 12 16 [2,] 10 14 18 Is there a way to create M without looping? Thanks a lot, Vicky Landsman. [[alternative HTML version deleted]]
try this: matSums <- function(lis, na.rm=FALSE){ if(!is.list(lis) || !all(sapply(lis, is.matrix))) stop("'lis' must be a list containing 2-dimensional arrays") dims <- sapply(lis, dim) n <- dims[1, 1] p <- dims[2, 1] if(!all(n == dims[1, ]) || !all(p == dims[2, ])) stop("the matrices must have the same dimensions") mat <- matrix(unlist(lis), n * p, length(lis)) matrix(rowSums(mat, na.rm=na.rm), n, p) } ######### mylist <- list(matrix(1:6, 2), matrix(7:12, 2)) matSums(mylist) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Vicky Landsman" <msvika at mscc.huji.ac.il> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, March 16, 2005 5:21 PM Subject: [R] Summing up matrices in a list> Dear all, > I think that my question is very simple but I failed to solve it. > I have a list which elements are matrices like this: > >>mylist > [[1]] > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > [[2]] > [,1] [,2] [,3] > [1,] 7 9 11 > [2,] 8 10 12 > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 > > Is there a way to create M without looping? > Thanks a lot, > Vicky Landsman. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Dear Vicky, Actually, this question was asked before (about a year ago, I think). Looping turns out to be not so bad a solution; check out the following example (from a short-course that I taught): ------- snip -------- # to loop or not to loop? # Example: summing a list of matrices matrices <- as.list(1:1000) # reasonable system.time(for (i in 1:1000) matrices[[i]] <- matrix(rnorm(100), 10, 10)) matrices <- list() # problematic system.time(for (i in 1:1000) matrices <- c(matrices, list(matrix(rnorm(100), 10, 10)))) S <- matrix(0, 10, 10) # simple system.time(for (i in 1:length(matrices)) S <- S + matrices[[i]]) S # "clever" system.time(S <- apply(array(unlist(matrices), dim = c(10, 10, 1000)), 1:2, sum)) S ------- snip -------- I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Vicky Landsman > Sent: Wednesday, March 16, 2005 11:22 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Summing up matrices in a list > > Dear all, > I think that my question is very simple but I failed to solve it. > I have a list which elements are matrices like this: > > >mylist > [[1]] > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > [[2]] > [,1] [,2] [,3] > [1,] 7 9 11 > [2,] 8 10 12 > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 > > Is there a way to create M without looping? > Thanks a lot, > Vicky Landsman. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:> Dear all, > I think that my question is very simple but I failed to solve it. > I have a list which elements are matrices like this: > > >mylist > [[1]] > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > [[2]] > [,1] [,2] [,3] > [1,] 7 9 11 > [2,] 8 10 12 > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 > > Is there a way to create M without looping? > Thanks a lot,> do.call("+", mylist)[,1] [,2] [,3] [1,] 8 12 16 [2,] 10 14 18 See ?do.call for more information. HTH, Marc Schwartz
Vicky Landsman wrote on 3/16/2005 10:21 AM:> Dear all, > I think that my question is very simple but I failed to solve it. > I have a list which elements are matrices like this: > > >>mylist > > [[1]] > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > [[2]] > [,1] [,2] [,3] > [1,] 7 9 11 > [2,] 8 10 12 > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 > > Is there a way to create M without looping? > Thanks a lot, > Vicky Landsman. > [[alternative HTML version deleted]] >Hi Vicky, This question is in the archives several times: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/10963.html My answer last time was the following: m <- list(matrix(rnorm(4),2,2), matrix(rnorm(4),2,2), matrix(rnorm(4),2,2)) mexpr <- paste("m[[", seq(along = m), "]]", collapse="+") eval(parse(text = mexpr)) HTH, --sundar
mylist <- list( matrix(1:6, nc=3), matrix(7:12, nc=3) ) do.call("+", mylist) [,1] [,2] [,3] [1,] 8 12 16 [2,] 10 14 18 Regards, Adai On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:> Dear all, > I think that my question is very simple but I failed to solve it. > I have a list which elements are matrices like this: > > >mylist > [[1]] > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > [[2]] > [,1] [,2] [,3] > [1,] 7 9 11 > [2,] 8 10 12 > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 > > Is there a way to create M without looping? > Thanks a lot, > Vicky Landsman. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Vicky Landsman <msvika <at> mscc.huji.ac.il> writes: : : Dear all, : I think that my question is very simple but I failed to solve it. : I have a list which elements are matrices like this: : : >mylist : [[1]] : [,1] [,2] [,3] : [1,] 1 3 5 : [2,] 2 4 6 : : [[2]] : [,1] [,2] [,3] : [1,] 7 9 11 : [2,] 8 10 12 : : I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] : [,1] [,2] [,3] : [1,] 8 12 16 : [2,] 10 14 18 : : Is there a way to create M without looping? : Thanks a lot, : Vicky Landsman. If there are n3 matrices each of dimension n1 x n2 then we can create an n1 x n2 x n3 array and apply sum over it: # get dimensions n1xn2 <- dim(mylist[[1]]) n3 <- length(mylist) # create 3d array and sum arr <- array(unlist(mylist), c(n1xn2, n3)) apply(arr, 1:2, sum)
On Wed, 16 Mar 2005, Adaikalavan Ramasamy wrote:> mylist <- list( matrix(1:6, nc=3), matrix(7:12, nc=3) ) > do.call("+", mylist) > [,1] [,2] [,3] > [1,] 8 12 16 > [2,] 10 14 18 >Yes, but this works only when the list is of length 2, when M<-mylist[[1]]+mylist[[2]] seems preferable. -thomas
Here's a slight variation:> lst <- list(matrix(1:4, 2, 2), matrix(5:8, 2, 2), matrix(9:12, 2, 2)) > m <- matrix(0, 2, 2) > m[] <- rowSums(do.call("cbind", lapply(lst, c))) > m[,1] [,2] [1,] 15 21 [2,] 18 24 This can probably be simplified even more with the `abind' package... Andy> From: Dimitris Rizopoulos > > try this: > > matSums <- function(lis, na.rm=FALSE){ > if(!is.list(lis) || !all(sapply(lis, is.matrix))) > stop("'lis' must > be a list containing 2-dimensional arrays") > dims <- sapply(lis, dim) > n <- dims[1, 1] > p <- dims[2, 1] > if(!all(n == dims[1, ]) || !all(p == dims[2, ])) stop("the > matrices must have the same dimensions") > mat <- matrix(unlist(lis), n * p, length(lis)) > matrix(rowSums(mat, na.rm=na.rm), n, p) > } > ######### > mylist <- list(matrix(1:6, 2), matrix(7:12, 2)) > matSums(mylist) > > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/16/336899 > Fax: +32/16/337015 > Web: http://www.med.kuleuven.ac.be/biostat/ > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Vicky Landsman" <msvika at mscc.huji.ac.il> > To: <r-help at stat.math.ethz.ch> > Sent: Wednesday, March 16, 2005 5:21 PM > Subject: [R] Summing up matrices in a list > > > > Dear all, > > I think that my question is very simple but I failed to solve it. > > I have a list which elements are matrices like this: > > > >>mylist > > [[1]] > > [,1] [,2] [,3] > > [1,] 1 3 5 > > [2,] 2 4 6 > > > > [[2]] > > [,1] [,2] [,3] > > [1,] 7 9 11 > > [2,] 8 10 12 > > > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]] > > [,1] [,2] [,3] > > [1,] 8 12 16 > > [2,] 10 14 18 > > > > Is there a way to create M without looping? > > Thanks a lot, > > Vicky Landsman. > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >