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) ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
