Reputation: 383
I have a list that contains multiple matrices (from 2 to n). For simplicity's sake, let's say I have a list of 5 matrices as follows.
> lst
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 1 1 1 1
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 2 2 2 2 2
[2,] 2 2 2 2 2
...
[[5]]
[,1] [,2] [,3] [,4] [,5]
[1,] 5 5 5 5 5
[2,] 5 5 5 5 5
How do I perform an operation (something like colSums but for lists) to add these together and get a single matrix as the output:
[,1] [,2] [,3] [,4] [,5]
[1,] 15 15 15 15 15
[2,] 15 15 15 15 15
I tried mapply(sum,lst[[1]],lst[[2]],lst[[3]],lst[[4]],lst[[5]])
. This does get me the sum, but the matrix loses its shape and requires naming each list item (maybe okay with 5, but what if I have 100?).
Upvotes: 1
Views: 519