r - Element-wise mean over list of matrices -
this question has answer here:
- mean of each element of list of matrices 2 answers
- how sum numeric list elements in r 1 answer
suppose have list of matrices. convenient way calculate mean matrix on element element basic? suppose have list of matrices:
> <- matrix(c(1:9), 3, 3) > [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > b <- matrix(c(2:10), 3, 3) > b [,1] [,2] [,3] [1,] 2 5 8 [2,] 3 6 9 [3,] 4 7 10 > my.list <- list(a, b)
so desired output should be:
[,1] [,2] [,3] [1,] 1.5 4.5 7.5 [2,] 2.5 5.5 8.5 [3,] 3.5 6.5 9.5
you can use:
reduce("+", my.list) / length(my.list)
according comments, want both mean
, sd
implemented on list of matrices, , above ways not work smoothly sd
. try instead :
apply(simplify2array(my.list), 1:2, mean) apply(simplify2array(my.list), 1:2, sd)
Comments
Post a Comment