如何计算R中数组中所有元素的矩阵积?
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]