[R] sum

Jerome Asselin jerome at hivnet.ubc.ca
Wed Apr 23 20:29:03 CEST 2003


The only way I can see is to vectorize your list of matrices. Here's an 
example.

matlist <- lapply(1:10,function(i) matrix(rnorm(12),3,4))
summat <- matrix(sapply(matlist,I)%*%rep(1,10),3,4)

You can use the loop below to verify that the above solution is correct.

forsum <- matrix(0,3,4)
for(i in 1:10)
forsum <- forsum+matlist[[i]]

"summat" and "forsum" should be the same.

HTH,
Jerome

On April 23, 2003 10:57 am, Luis Silva wrote:
> Dear helpers
>
> I have a list where each element is a matrix (the list is
> obtained with lapply). I want to sum those matrices. Is there a
> function to do that? The sum function sums all the elements...



More information about the R-help mailing list