I have 'mylist" - a list of same size matrices:
mylist <- vector("list", 5)
set.seed(123)
for(i in 1:5){
mylist[[i]] <- matrix(rnorm(9), nrow = 3)
}
I also have a vector of weights 'mywgts' - same length as 'mylist'
mywgts <- c(0.8, 0.9, 1, 1.1, 1.2)
I need to calculate a weighted mean of these matrices - element by element. The result will be a 3 by 3 matrix where the first element is:
mylist[[1]][1,1]*mywgts[1] + mylist[[2]][1,1]*mywgts[2] +
mylist[[3]][1,1]*mywgts[3] + mylist[[4]][1,1]*mywgts[4] +
mylist[[5]][1,1]*mywgts[5]
I know how to do it by looping through all elements of a matrix. But I am looking for a more parsimonious/elegant R-like solution. Also - the actual length of 'mylist' is not known in advance.
Thank you for any hint!