[R] Multiply a 3D-array by a vector (weighted combination of matrices)
Yvonnick NOEL
yvonnick.noel at uhb.fr
Wed Oct 17 09:58:53 CEST 2007
Hello,
I would like to compute a weighted combination of matrices.
I have a number of matrices, arranged in a 3D-array, say:
z = array(rep(1:3,c(9,9,9)),c(3,3,3))
so that z[,,1] is my first matrix, and z[,,2] and z[,,3] the second and
third one, and a vector of coefficients:
w = rep(1/3,3)
I would like to compute:
w[1]* z[,,1] + w[2]*z[,,2] + w[3]*z[,,3]
I could of course do this using a for() loop, but would like to know if
there is a way to do it in a "vectorized" manner, or any other way that
is likely to result in faster computation.
Any hint ?
Thank you very much in advance,
YNOEL
More information about the R-help
mailing list