It's interesting that sweep is the slowest one comparing to replicate and rep
:)
-
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-tp2315537p2316586.html
Sent from the R help mailing list archive a
5, 2010 2:00 PM
> To: r-help@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[, ,
I have only achieved a half improvement.
x <- array(1:2400*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:2400*1, dim = c(200,300,400))
y <- 1:400*1
ptm <- proc.time
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(
4 matches
Mail list logo