Hi, I look for a solution to the following problem: I have a (n x p) matrix X and a (p x 1) vector Y which are to be multiplied such the result is the (n x p) matrix Z = ( X[,1]*Y[1] .... X[,p]*Y[p] ) Up to now I found that sweep(X,2,Y,"*") is (almost) what I want, but only for p>1. Is there another elegant way to do this by matrix operations -- including the case p=1? -- Marlene Mueller http://www.uni-bayreuth.de/departments/math/org/mathe7/Mueller.html http://ise.wiwi.hu-berlin.de/~marlene -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
You could do rep(Y,rep(n,p))*X. The outer "rep" expands Y by replacing each entry with a number of copies determined by the corresponding entry in the second argument. Here the second argument is rep(n,p) so the number of copies is always n. Reid Huntsinger -----Original Message----- From: Marlene Mueller [mailto:Marlene.Mueller at gmx.de] Sent: Wednesday, May 23, 2001 1:10 PM To: r-help at stat.math.ethz.ch Subject: [R] problem with matrix multiplication Hi, I look for a solution to the following problem: I have a (n x p) matrix X and a (p x 1) vector Y which are to be multiplied such the result is the (n x p) matrix Z = ( X[,1]*Y[1] .... X[,p]*Y[p] ) Up to now I found that sweep(X,2,Y,"*") is (almost) what I want, but only for p>1. Is there another elegant way to do this by matrix operations -- including the case p=1? -- Marlene Mueller http://www.uni-bayreuth.de/departments/math/org/mathe7/Mueller.html http://ise.wiwi.hu-berlin.de/~marlene -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks for all solutions! The most elegant & efficient way seems to me t(t(X)*Y) or t(Y*t(X)). Marlene Marlene Mueller wrote:> > Hi, I look for a solution to the following problem: > > I have a (n x p) matrix X and a (p x 1) vector Y which > are to be multiplied such the result is the (n x p) matrix > > Z = ( X[,1]*Y[1] .... X[,p]*Y[p] ) > > Up to now I found that sweep(X,2,Y,"*") is (almost) what > I want, but only for p>1. > > Is there another elegant way to do this by matrix > operations -- including the case p=1? > > -- > Marlene Mueller > http://www.uni-bayreuth.de/departments/math/org/mathe7/Mueller.html > http://ise.wiwi.hu-berlin.de/~marlene > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Marlene Mueller http://www.uni-bayreuth.de/departments/math/org/mathe7/Mueller.html http://ise.wiwi.hu-berlin.de/~marlene -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._