Jason Liao
2001-Sep-04 01:48 UTC
[R] fastest way to multiply each column of a matrix by a single vlaue
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 anything else? I have a program that uses this kind of operation million of times and I appreciate your input. Thanks. Jason Liao ====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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2001-Sep-04 05:49 UTC
[R] fastest way to multiply each column of a matrix by a single vlaue
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/BYou 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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Philippe Grosjean
2001-Sep-04 09:12 UTC
[R] fastest way to multiply each column of a matrix by a single vlaue
Why not make two versions of your program with each alternative and test their speed with system.time()? Then, you'll have this indication "in situation". Best regards, Philippe Grosjean ...........]<(({?<...............<?}))><............................... ) ) ) ) ) __ __ ( ( ( ( ( |__) | _ ) ) ) ) ) | hilippe |__)rosjean ( ( ( ( ( Marine Biol. Lab., ULB, Belgium ) ) ) ) ) __ ( ( ( ( ( |\ /| |__) ) ) ) ) ) | \/ |ariculture & |__)iostatistics ( ( ( ( ( ) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org ( ( ( ( ( SciViews project coordinator (http://www.sciviews.org) ) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home) ( ( ( ( ( ) ) ) ) ) "I'm 100% confident that p is between 0 and 1" ( ( ( ( ( L. Gonick & W. Smith (1993) ) ) ) ) ) .......................................................................>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>anything else?>I have a program that uses this kind of operation million of times and >I appreciate your input.>Thanks.>Jason Liao====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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._