multiply every member of a R list (matrices) by each other

r

Solution

Use `Reduce` if you want an element-by-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 `simplify2array` function and `apply`

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

If you want to use `abind` then use the following:

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

Problem

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 having to type it out by hand (my list has dozens of matrices).

Original source