If I have two matrices like x <- matrix(rep(c(1,2,3),3),3) y <- matrix(rep(c(4,5,6),3),3) How can I combine them to get ? 1 1 1 4 4 4 1 1 1 5 5 5 1 1 1 6 6 6 2 2 2 4 4 4 2 2 2 5 5 5 2 2 2 6 6 6 3 3 3 4 4 4 3 3 3 5 5 5 3 3 3 6 6 6 The number of rows and the actual numbers above are unimportant, they are given so as to illustrate how I want to combine the matrices. I.e., I am looking for a general way to combine the first row of x with each row of y, then the second row of x with y, .... Thanks, Dan Daniel Nordlund Bothell, WA USA Thanks for
Try this; do.call(rbind, lapply(split(x, seq(nrow(x))), cbind, y)) On Mon, Aug 24, 2009 at 1:16 PM, Daniel Nordlund <djnordlund@verizon.net>wrote:> If I have two matrices like > > x <- matrix(rep(c(1,2,3),3),3) > y <- matrix(rep(c(4,5,6),3),3) > > How can I combine them to get ? > > 1 1 1 4 4 4 > 1 1 1 5 5 5 > 1 1 1 6 6 6 > 2 2 2 4 4 4 > 2 2 2 5 5 5 > 2 2 2 6 6 6 > 3 3 3 4 4 4 > 3 3 3 5 5 5 > 3 3 3 6 6 6 > > The number of rows and the actual numbers above are unimportant, they are > given so as to illustrate how I want to combine the matrices. I.e., I am > looking for a general way to combine the first row of x with each row of y, > then the second row of x with y, .... > > Thanks, > > Dan > > Daniel Nordlund > Bothell, WA USA > > Thanks for > > ______________________________________________ > R-help@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 [[alternative HTML version deleted]]
On Aug 24, 2009, at 11:16 AM, Daniel Nordlund wrote:> If I have two matrices like > > x <- matrix(rep(c(1,2,3),3),3) > y <- matrix(rep(c(4,5,6),3),3) > > How can I combine them to get ? > > 1 1 1 4 4 4 > 1 1 1 5 5 5 > 1 1 1 6 6 6 > 2 2 2 4 4 4 > 2 2 2 5 5 5 > 2 2 2 6 6 6 > 3 3 3 4 4 4 > 3 3 3 5 5 5 > 3 3 3 6 6 6 > > The number of rows and the actual numbers above are unimportant, > they are given so as to illustrate how I want to combine the > matrices. I.e., I am looking for a general way to combine the first > row of x with each row of y, then the second row of x with y, .... > > Thanks, > > Dannr.x <- nrow(x) nr.y <- nrow(y) > cbind(x[rep(1:nr.x, each = nr.x), ], y[rep(1:nr.y, nr.y), ]) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 4 4 4 [2,] 1 1 1 5 5 5 [3,] 1 1 1 6 6 6 [4,] 2 2 2 4 4 4 [5,] 2 2 2 5 5 5 [6,] 2 2 2 6 6 6 [7,] 3 3 3 4 4 4 [8,] 3 3 3 5 5 5 [9,] 3 3 3 6 6 6 HTH, Marc Schwartz
On Aug 24, 2009, at 11:46 AM, Marc Schwartz wrote:> > On Aug 24, 2009, at 11:16 AM, Daniel Nordlund wrote: > >> If I have two matrices like >> >> x <- matrix(rep(c(1,2,3),3),3) >> y <- matrix(rep(c(4,5,6),3),3) >> >> How can I combine them to get ? >> >> 1 1 1 4 4 4 >> 1 1 1 5 5 5 >> 1 1 1 6 6 6 >> 2 2 2 4 4 4 >> 2 2 2 5 5 5 >> 2 2 2 6 6 6 >> 3 3 3 4 4 4 >> 3 3 3 5 5 5 >> 3 3 3 6 6 6 >> >> The number of rows and the actual numbers above are unimportant, >> they are given so as to illustrate how I want to combine the >> matrices. I.e., I am looking for a general way to combine the >> first row of x with each row of y, then the second row of x with >> y, .... >> >> Thanks, >> >> Dan > > > > nr.x <- nrow(x) > nr.y <- nrow(y) > > > cbind(x[rep(1:nr.x, each = nr.x), ], y[rep(1:nr.y, nr.y), ]) > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 1 1 1 4 4 4 > [2,] 1 1 1 5 5 5 > [3,] 1 1 1 6 6 6 > [4,] 2 2 2 4 4 4 > [5,] 2 2 2 5 5 5 > [6,] 2 2 2 6 6 6 > [7,] 3 3 3 4 4 4 > [8,] 3 3 3 5 5 5 > [9,] 3 3 3 6 6 6Actually, correction...that will work in this case, but in the general case, I believe that it needs to be: x <- matrix(rep(c(1,2,3),3),3) y <- matrix(rep(c(4,5,6,7),3),4) > x [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 > y [,1] [,2] [,3] [1,] 4 4 4 [2,] 5 5 5 [3,] 6 6 6 [4,] 7 7 7 nr.x <- nrow(x) nr.y <- nrow(y) > cbind(x[rep(1:nr.x, each = nr.y), ], y[rep(1:nr.y, nr.x), ]) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 4 4 4 [2,] 1 1 1 5 5 5 [3,] 1 1 1 6 6 6 [4,] 1 1 1 7 7 7 [5,] 2 2 2 4 4 4 [6,] 2 2 2 5 5 5 [7,] 2 2 2 6 6 6 [8,] 2 2 2 7 7 7 [9,] 3 3 3 4 4 4 [10,] 3 3 3 5 5 5 [11,] 3 3 3 6 6 6 [12,] 3 3 3 7 7 7 We need to replicate each row by the number of rows in the other matrix. HTH, Marc
> -----Original Message----- > From: Marc Schwartz [mailto:marc_schwartz at me.com] > Sent: Monday, August 24, 2009 9:57 AM > To: Daniel Nordlund > Cc: r help > Subject: Re: [R] Combining matrices > > > On Aug 24, 2009, at 11:46 AM, Marc Schwartz wrote: > > > > > On Aug 24, 2009, at 11:16 AM, Daniel Nordlund wrote: > > > >> If I have two matrices like > >> > >> x <- matrix(rep(c(1,2,3),3),3) > >> y <- matrix(rep(c(4,5,6),3),3) > >> > >> How can I combine them to get ? > >> > >> 1 1 1 4 4 4 > >> 1 1 1 5 5 5 > >> 1 1 1 6 6 6 > >> 2 2 2 4 4 4 > >> 2 2 2 5 5 5 > >> 2 2 2 6 6 6 > >> 3 3 3 4 4 4 > >> 3 3 3 5 5 5 > >> 3 3 3 6 6 6 > >> > >> The number of rows and the actual numbers above are unimportant, > >> they are given so as to illustrate how I want to combine the > >> matrices. I.e., I am looking for a general way to combine the > >> first row of x with each row of y, then the second row of x with > >> y, .... > >> > >> Thanks, > >> > >> Dan > > > > > > > > nr.x <- nrow(x) > > nr.y <- nrow(y) > > > > > cbind(x[rep(1:nr.x, each = nr.x), ], y[rep(1:nr.y, nr.y), ]) > > [,1] [,2] [,3] [,4] [,5] [,6] > > [1,] 1 1 1 4 4 4 > > [2,] 1 1 1 5 5 5 > > [3,] 1 1 1 6 6 6 > > [4,] 2 2 2 4 4 4 > > [5,] 2 2 2 5 5 5 > > [6,] 2 2 2 6 6 6 > > [7,] 3 3 3 4 4 4 > > [8,] 3 3 3 5 5 5 > > [9,] 3 3 3 6 6 6 > > > > Actually, correction...that will work in this case, but in > the general > case, I believe that it needs to be: > > x <- matrix(rep(c(1,2,3),3),3) > y <- matrix(rep(c(4,5,6,7),3),4) > > > x > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 2 2 2 > [3,] 3 3 3 > > > y > [,1] [,2] [,3] > [1,] 4 4 4 > [2,] 5 5 5 > [3,] 6 6 6 > [4,] 7 7 7 > > > nr.x <- nrow(x) > nr.y <- nrow(y) > > > > cbind(x[rep(1:nr.x, each = nr.y), ], y[rep(1:nr.y, nr.x), ]) > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 1 1 1 4 4 4 > [2,] 1 1 1 5 5 5 > [3,] 1 1 1 6 6 6 > [4,] 1 1 1 7 7 7 > [5,] 2 2 2 4 4 4 > [6,] 2 2 2 5 5 5 > [7,] 2 2 2 6 6 6 > [8,] 2 2 2 7 7 7 > [9,] 3 3 3 4 4 4 > [10,] 3 3 3 5 5 5 > [11,] 3 3 3 6 6 6 > [12,] 3 3 3 7 7 7 > > > We need to replicate each row by the number of rows in the > other matrix. > > HTH, > > Marc >Thanks to all who responded (including those off-list). I now have options to apply to solving my programming task. Thanks, Dan
Try this: kronecker(cbind(x, y), rep(1, 3)) On Mon, Aug 24, 2009 at 12:16 PM, Daniel Nordlund<djnordlund at verizon.net> wrote:> If I have two matrices like > > x <- matrix(rep(c(1,2,3),3),3) > y <- matrix(rep(c(4,5,6),3),3) > > How can I combine ?them to get ? > > 1 1 1 4 4 4 > 1 1 1 5 5 5 > 1 1 1 6 6 6 > 2 2 2 4 4 4 > 2 2 2 5 5 5 > 2 2 2 6 6 6 > 3 3 3 4 4 4 > 3 3 3 5 5 5 > 3 3 3 6 6 6 > > The number of rows and the actual numbers above are unimportant, they are given so as to illustrate how I want to combine the matrices. ?I.e., I am looking for a general way to combine the first row of x with each row of y, then the second row of x with y, .... > > Thanks, > > Dan > > Daniel Nordlund > Bothell, WA USA > > Thanks for > > ______________________________________________ > 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. >
My prior solution was not correct.
If the idea is to combine each row of x with each row
of y then convert the matrices to data frames and
perform and outer join with SQL like this:
library(sqldf)
X <- as.data.frame(x)
Y <- as.data.frame(y)
as.matrix(sqldf("select * from X, Y", method = "raw"))
See http://sqdfl.googlecode.com
On Mon, Aug 24, 2009 at 1:10 PM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:> Try this:
>
> kronecker(cbind(x, y), rep(1, 3))
>
>
> On Mon, Aug 24, 2009 at 12:16 PM, Daniel Nordlund<djnordlund at
verizon.net> wrote:
>> If I have two matrices like
>>
>> x <- matrix(rep(c(1,2,3),3),3)
>> y <- matrix(rep(c(4,5,6),3),3)
>>
>> How can I combine ?them to get ?
>>
>> 1 1 1 4 4 4
>> 1 1 1 5 5 5
>> 1 1 1 6 6 6
>> 2 2 2 4 4 4
>> 2 2 2 5 5 5
>> 2 2 2 6 6 6
>> 3 3 3 4 4 4
>> 3 3 3 5 5 5
>> 3 3 3 6 6 6
>>
>> The number of rows and the actual numbers above are unimportant, they
are given so as to illustrate how I want to combine the matrices. ?I.e., I am
looking for a general way to combine the first row of x with each row of y, then
the second row of x with y, ....
>>
>> Thanks,
>>
>> Dan
>>
>> Daniel Nordlund
>> Bothell, WA USA
>>
>> Thanks for
>>
>> ______________________________________________
>> 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.
>>
>