Multiply each element of the list R (matrices) by each other

I have a list of equally sized matrices in R that I want to multiply by each other.

I am looking for a way to do:

list$A * list$B * list$C * ... 

Without entering it manually (my list contains dozens of matrices).

+8
r
source share
1 answer

Use Reduce if you need element multiplication

 > Lists <- list(matrix(1:4, 2), matrix(5:8, 2), matrix(10:13, 2)) > Reduce("*", Lists) [,1] [,2] [1,] 50 252 [2,] 132 416 

Instead of using abind you can use the function simplify2array and apply

 > apply(simplify2array(Lists), c(1,2), prod) [,1] [,2] [1,] 50 252 [2,] 132 416 

If you want to use abind , use the following:

 > library(abind) > apply(abind(Lists, along=3), c(1,2), prod) [,1] [,2] [1,] 50 252 [2,] 132 416 
+12
source share

All Articles