[R] array construction, avoiding for loop
David Firth
david.firth at nuffield.oxford.ac.uk
Tue Jul 16 19:28:29 CEST 2002
Suppose X has dimensions (I,J), and Y has dimensions (J,K). To construct
a 3-way array whose (i,j,k)th element is X[i,j]*Y[j,k], is there something
much more economical than
XY <- outer(X,Y)
result <- array(0, c(I,J,K))
for (j in 1:J){
result[,j,] <- XY[,j,j,]
}
This is faster than using three nested for loops, but it still has a for
loop and a fair amount of redundant calculation/storage for the 4-way
array XY.
I need to do this lots of times (hence interested in speed), but my arrays
are typically not large (I, J, K of the order of 10).
I feel I must be missing some trick here. Any hints welcomed.
David
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list