On 4/16/05, GiusVa <[EMAIL PROTECTED]> wrote:
> Dear members,
> 
> The code I am writing heavily use element-wise multiplication of
> matrix and vectors, e.g.
> 
> X, is nxm matrix
> e, is nx1 matrix
> 
> Doing Z=X*e[,], I obtain a nxm matrix, Z, where each column of X is
> multiplied (element-wise) by e. Is this the best way to achieve the
> result I want? By best way, I mean the fastest way. By profiling my
> code, 45% of the time is spent by "*" and even a 30% speedup in
> obtaining Z would greatly benefit the total speed of the code.
> 
> Aby suggestion is greatly appreciated.
> 
> |Giuseppe Ragusa
> |University of California, San Diego

Try this:

> mm <- matrix(1, 1000, 1000)
> ee <- matrix(1:100,nc=1)

> system.time(mm*ee[,], TRUE)
[1] 0.26 0.02 0.28   NA   NA

> system.time(mm*c(ee), TRUE)
[1] 0.07 0.00 0.07   NA   NA

> # if you have to do it multiple times with same ee:
> cee <- c(ee)
> system.time(mm*cee, TRUE)
[1] 0.06 0.00 0.06   NA   NA

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to