如何在R中将数组中所有元素的矩阵乘积取出?
我已经在Stack Exchange上搜索过,但没有发现任何结果。
n <- 3
ARRAY <- array(NA, dim = c(4, 4, n))
for (i in 1:4) {
for (j in 1:4) {
ARRAY[i, j, ] <- rnorm(n)
}
}
# this gives me a 1x1 matrix, which is wrong
Reduce("%*%", ARRAY)
mapply("%*%", ARRAY[,,1], ARRAY[,,2])
# this works, but I'd like a generalizable option
ARRAY[,,1]%*%ARRAY[,,2]%*%ARRAY[,,3]