Related to this question: Multiply multidimensional array with same-sized matrix
If I create the following array and matrix:
a <- array(1, dim=c(2,2,3))
b <- matrix(c(1,1,1,1), nrow=2)
and would like to carray out an elementwise multiplication of b with each slice of a moving along the 3rd dimension, I would use apply. However, I get the following strange result:
> dim(apply(a, 3, `*`, b))
[1] 4 3
> newa <- array(0, dim=c(2,2,3))
> newa[] <- apply(a, 3, `*`, b)
> dim(newa)
[1] 2 2 3
Why do these two things give different answers?