Hi, this may sound stupid (and it probably is), but I can't seem to find out how to multiply each element of a vector with each element of another vector where the result would be a matrix of dim[length(vectorOne),length(vectorTwo)] without using loops. example: if a -> c(1,2,3) b -> c(4,5,6) i'm looking for the operation that would yield: [,1] [,2] [,3] [1,] 4 8 12 [2,] 5 10 15 [3,] 6 12 18 any hints are greatly appreciated, kaspar --------------------------------------------- kaspar bernet kbernet at student.ethz.ch stationsstrasse 32 8442 hettlingen tel 052 301 12 33 mobil 079 789 75 61
Try this: b %o% a On Wed, Mar 17, 2010 at 3:03 PM, Kaspar Bernet <kbernet at student.ethz.ch> wrote:> Hi, > > this may sound stupid (and it probably is), but I can't seem to find out how to multiply each element of a vector with each element of another vector where the result would be a matrix of dim[length(vectorOne),length(vectorTwo)] without using loops. > > example: if > > a -> c(1,2,3) > b -> c(4,5,6) > > i'm looking for the operation that would yield: > > ? ?[,1] [,2] [,3] > [1,] ? ?4 ? ?8 ? 12 > [2,] ? ?5 ? 10 ? 15 > [3,] ? ?6 ? 12 ? 18 > > any hints are greatly appreciated, > kaspar > > --------------------------------------------- > kaspar bernet kbernet at student.ethz.ch > stationsstrasse 32 8442 hettlingen > tel 052 301 12 33 mobil 079 789 75 61 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Tena koe Kaspar ?outer outer(b, a) or b %o% a HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Kaspar Bernet > Sent: Thursday, 18 March 2010 7:03 a.m. > To: r-help at r-project.org > Subject: [R] Vector multiplication > > Hi, > > this may sound stupid (and it probably is), but I can't seem to find > out how to multiply each element of a vector with each element of > another vector where the result would be a matrix of > dim[length(vectorOne),length(vectorTwo)] without using loops. > > example: if > > a -> c(1,2,3) > b -> c(4,5,6) > > i'm looking for the operation that would yield: > > [,1] [,2] [,3] > [1,] 4 8 12 > [2,] 5 10 15 > [3,] 6 12 18 > > any hints are greatly appreciated, > kaspar > > --------------------------------------------- > kaspar bernet kbernet at student.ethz.ch > stationsstrasse 32 8442 hettlingen > tel 052 301 12 33 mobil 079 789 75 61 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
On 17/03/2010 2:03 PM, Kaspar Bernet wrote:> Hi, > > this may sound stupid (and it probably is), but I can't seem to find out how to multiply each element of a vector with each element of another vector where the result would be a matrix of dim[length(vectorOne),length(vectorTwo)] without using loops. > > example: if > > a -> c(1,2,3) > b -> c(4,5,6) > > i'm looking for the operation that would yield: > > [,1] [,2] [,3] > [1,] 4 8 12 > [2,] 5 10 15 > [3,] 6 12 18 > > any hints are greatly appreciated,> a <- 1:3 > b <- 4:6 > outer(a,b) [,1] [,2] [,3] [1,] 4 5 6 [2,] 8 10 12 [3,] 12 15 18 > outer(b,a) [,1] [,2] [,3] [1,] 4 8 12 [2,] 5 10 15 [3,] 6 12 18 Duncan Murdoch