Given a list of matrices, returns a single matrix in which element (i, j) is the sum of all corresponding elements (i, j) of all matrices in the list. Given a list of vectors, returns a single vector in which element i is the sum of all corresponding elements i of all vectors in the list.
Value
A matrix in which element (i,j) is the sum of all corresponding elements (i,j) of all matrices in the original list
Examples
mat1 <- matrix(1:4, nrow = 2)
mat2 <- matrix(5:8, nrow = 2)
mat3 <- matrix(9:12, nrow = 2)
list_matrix <- list(mat1, mat2, mat3)
list_matrix
#> [[1]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
sumMatrix(list_matrix)
#> [,1] [,2]
#> [1,] 15 21
#> [2,] 18 24