I have a list of matrices of equal size:
a <- matrix(data = c(1,2,3,4,5,6,7,8,9), ncol = 3, nrow = 3)
b <- matrix(data = c(9,8,7,6,5,4,3,2,1), ncol = 3, nrow = 3)
c <- matrix(data = c(1,2,3,4,5,6,7,8,9), ncol = 3, nrow = 3)
d <- matrix(data = seq(from = 1, to = 9, by = 1), ncol = 3, nrow = 3)
e <- matrix(data = seq(from = 10, to = 90, by = 10), ncol = 3, nrow = 3)
f <- matrix(data = seq(from = 9, to = 1, by = -1), ncol = 3, nrow = 3)
test_list <- list(a, b, c, d, e, f)
How can I sum every set of three matrices so that the output is two matrices, the first being the sum of a
, b
and c
(output_1
) and the second the sum of d
, e
and f
(output_2
)? Ideally the output would be a new list of two matrices, e.g.
output_1 <- structure(c(11, 12, 13, 14, 15, 16, 17, 18, 19), .Dim = c(3L,
3L))
output_2 <- structure(c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 24L, 27L), .Dim = c(3L,
3L))