我在R中有一个相同大小的矩阵列表,我希望彼此相乘.
我正在寻找一种方法:
list$A * list$B * list$C * ...
Run Code Online (Sandbox Code Playgroud)
无需手动输入(我的列表有几十个矩阵).
Jil*_*ina 16
使用Reduce如果你想要一个元素逐个元素相乘
> Lists <- list(matrix(1:4, 2), matrix(5:8, 2), matrix(10:13, 2))
> Reduce("*", Lists)
[,1] [,2]
[1,] 50 252
[2,] 132 416
Run Code Online (Sandbox Code Playgroud)
而不是使用abind你可以使用simplify2array功能和apply
> apply(simplify2array(Lists), c(1,2), prod)
[,1] [,2]
[1,] 50 252
[2,] 132 416
Run Code Online (Sandbox Code Playgroud)
如果要使用,请abind使用以下内容:
> library(abind)
> apply(abind(Lists, along=3), c(1,2), prod)
[,1] [,2]
[1,] 50 252
[2,] 132 416
Run Code Online (Sandbox Code Playgroud)