Maurits Aben
2010-Aug-05 19:59 UTC
[R] 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 time (on larger arrays) and I suspect there should be solution with less computation time. Is there a faster method to perform this calculation (thus to multiply the depth of an array with respective vector elements)? Best regards, Maurits Aben [[alternative HTML version deleted]]
Wu Gong
2010-Aug-06 03:34 UTC
[R] Multiply each depth level of an array with another vector element
I have only achieved a half improvement. x <- array(1:24000000*1, dim = c(200,300,400)) y <- 1:400*1 ptm <- proc.time() z <- x*as.vector(t(replicate(dim(x)[1]*dim(x)[2], y[1:dim(x)[3]]))) "replicate:" proc.time() - ptm x <- array(1:24000000*1, dim = c(200,300,400)) y <- 1:400*1 ptm <- proc.time() z <- x*rep(y[1:dim(x)[3]],each=dim(x)[1]*dim(x)[2]) "rep:" proc.time() - ptm ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Multiply-each-depth-level-of-an-array-with-another-vector-element-tp2315537p2315860.html Sent from the R help mailing list archive at Nabble.com.
Greg Snow
2010-Aug-06 16:56 UTC
[R] Multiply each depth level of an array with another vector element
?sweep -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Maurits Aben > Sent: Thursday, August 05, 2010 2:00 PM > To: r-help at r-project.org > Subject: [R] 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 > time (on > larger arrays) and I suspect there should be solution with less > computation > time. Is there a faster method to perform this calculation (thus to > multiply > the depth of an array with respective vector elements)? > > > > Best regards, > > Maurits Aben > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Seemingly Similar Threads
- Error building package: LaTeX error when creating PDF version
- very fast OLS regression?
- Howto compile ocfs2-1.3.9-0.1 for CentOS5.2 2.6.18-92.1.6.el5xen +compile messages
- [LLVMdev] Instantiating Target-Specifc ASM Parser
- [Bug 2565] New: High baud rate gets sent, solaris closes pty