[R] 2 questions on matrix manipulation in R

Lei Liu liulei at virginia.edu
Mon Aug 8 06:18:37 CEST 2011


Hi there,

I have two questions on matrix manipulation. For the first one, I 
want to calculate the product of each column of a matrix (say A) with 
another vector (say b). That is, if A has 5 columns (a1, a2, a3, a4, 
a5), I want to obtain a matrix with columns (a1*b, a2*b, aA3*b, a4*b, 
a5*b). Can I do it directly, without using "for" loop?

For the second one, I have a matrix A of dimension 2 by n (say 
columns of a1, a2, ..., an), another matrix B of dimension 2 by 2. I 
want to obtain the vector with elements (t(a1) %*% B %*% a1, t(a2) 
%*% B %*% a2, ..., t(an) %*% B %*% an). Can I do it without using "for" loop?

Thanks a lot!

Lei Liu
Associate Professor
Division of Biostatistics
Department of Public Health Sciences
University of Virginia School of Medicine

http://people.virginia.edu/~ll9f/



More information about the R-help mailing list