[R] fastest way to multiply each column of a matrix by a single vlaue

Jason Liao jg_liao at yahoo.com
Tue Sep 4 03:48:02 CEST 2001

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:


solution 2:

B <- matrix( rep(b, m), byrow=T, nrow=m )

anything else?

I have a program that uses this kind of operation million of times and
I appreciate your input.


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


Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
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