[R] fastest way to multiply each column of a matrix by a single vlaue
Jason Liao
jg_liao at yahoo.com
Tue Sep 4 17:13:55 CEST 2001
Thanks, Prof. Ripley! That is really helpful.
Jason Liao
--- Prof Brian D Ripley <ripley at stats.ox.ac.uk> wrote:
> On Mon, 3 Sep 2001, Jason Liao wrote:
>
> > Let A be a m by n matrix and b a length n vector. What is the
> fastest
> > vectorized code for doing
> >
> > for(j in 1:n) A[, j] <- A[, j]/b[j] ?
> >
> > solution 1:
> >
> > t(t(A)/b)
> >
> > solution 2:
> >
> > B <- matrix( rep(b, m), byrow=T, nrow=m )
> > A/B
>
> You don't need the rep because of the re-cycling rule. Either
>
> B <- matrix(b, m, n, byrow=T)
>
> or, a litle faster
>
> A/rep(b, rep(m, n))
>
>
> You'll find the S/R system code uses the last most often.
>
> [...]
>
> --
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272860 (secr)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
>
=====
Jason G. Liao
Department of Biometry and Epidemiology
Medical University of South Carolina
135 Rutledge Ave., STE 1148, Charleston, SC 29425
phone (843) 876-1114, fax (843) 876-1126
http://www.geocities.com/jg_liao/index.html
__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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