Displaying 1 result from an estimated 1 matches for "tp2315537p2315860".
2010 Aug 05
2
Multiply each depth level of an array with another vector element
Suppose
x <- array(c(1,1,1,1,2,2,2,2), dim = c(2,2,2))
y <- c(5, 10)
Now I would like to multiply x[, , 1] with y[1] and x[, , 2] with y[2].
Possible solution is a for-loop:
for (i in 1:2) {
x[, , i] * y[i]
}
Another possible solution is this construction:
as.vector(t(replicate(nrow(x) * ncol(x), y))) * x
I find that these two solutions have a relatively large computation