Hi, I have a very basic question on merging two matrices by alternating the rows. For illustration, assume two matrices - A looks like: 10 10 10 10 B looks like: 20 20 20 20 How do I combine them such that I get alternating rows from A and B? My final result should be C which looks like: 10 10 20 20 10 10 20 20 Thanks very much, and I am sorry for such a newbie question. Shruthi -- View this message in context: http://www.nabble.com/Matrix-tp21792064p21792064.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 02.02.2009 16:52:06:> > Hi, > > I have a very basic question on merging two matrices by alternating the > rows. For illustration, assume two matrices - > > A looks like: > > 10 10 > 10 10 > > B looks like: > > 20 20 > 20 20 > > How do I combine them such that I get alternating rows from A and B? My > final result should be C which looks like: > > 10 10 > 20 20 > 10 10 > 20 20I would rbind both matrices and then indexed the resulting matrix. x<-rep(10,4) y<-rep(20,4) dim(x)<-c(2,2) dim(y)<-c(2,2) z<-rbind(x,y)> z[,1] [,2] [1,] 10 10 [2,] 10 10 [3,] 20 20 [4,] 20 20 ind<-c(seq(1,4,2), seq(2,4,2)) z[ind,]> z[ind,][,1] [,2] [1,] 10 10 [2,] 20 20 [3,] 10 10 [4,] 20 20 Regards Petr> > Thanks very much, and I am sorry for such a newbie question. > > Shruthi > -- > View this message in context:http://www.nabble.com/Matrix-tp21792064p21792064.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On Mon, Feb 2, 2009 at 4:05 PM, Petr PIKAL <petr.pikal at precheza.cz> wrote:> Hi > > r-help-bounces at r-project.org napsal dne 02.02.2009 16:52:06: > >> >> Hi, >> >> I have a very basic question on merging two matrices by alternating the >> rows. For illustration, assume two matrices - >> >> A looks like: >> >> 10 10 >> 10 10 >> >> B looks like: >> >> 20 20 >> 20 20 >> >> How do I combine them such that I get alternating rows from A and B? My >> final result should be C which looks like: >> >> 10 10 >> 20 20 >> 10 10 >> 20 20 > > I would rbind both matrices and then indexed the resulting matrix. > > x<-rep(10,4) > y<-rep(20,4) > dim(x)<-c(2,2) > dim(y)<-c(2,2) > z<-rbind(x,y) >> z > [,1] [,2] > [1,] 10 10 > [2,] 10 10 > [3,] 20 20 > [4,] 20 20 > > ind<-c(seq(1,4,2), seq(2,4,2)) > z[ind,] > >> z[ind,] > [,1] [,2] > [1,] 10 10 > [2,] 20 20 > [3,] 10 10 > [4,] 20 20 >Another solution: m1 <- matrix(10,4,2) m2 <- matrix(20,2,2) m1[seq(2,4,2),] <- m2 Paul
Try this: matrix(rbind(x, y), nc = 2, byrow = TRUE) On Mon, Feb 2, 2009 at 10:52 AM, Shruthi Jayaram <shruthi.jayaram.85 at gmail.com> wrote:> > Hi, > > I have a very basic question on merging two matrices by alternating the > rows. For illustration, assume two matrices - > > A looks like: > > 10 10 > 10 10 > > B looks like: > > 20 20 > 20 20 > > How do I combine them such that I get alternating rows from A and B? My > final result should be C which looks like: > > 10 10 > 20 20 > 10 10 > 20 20 > > Thanks very much, and I am sorry for such a newbie question. > > Shruthi > -- > View this message in context: http://www.nabble.com/Matrix-tp21792064p21792064.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Sorry, there was an error. Try this: x <- matrix(1:4, 2) y <- 10 * x matrix(t(cbind(x, y)), nc = 2, byrow = TRUE) On Mon, Feb 2, 2009 at 12:32 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try this: > > matrix(rbind(x, y), nc = 2, byrow = TRUE) > > > On Mon, Feb 2, 2009 at 10:52 AM, Shruthi Jayaram > <shruthi.jayaram.85 at gmail.com> wrote: >> >> Hi, >> >> I have a very basic question on merging two matrices by alternating the >> rows. For illustration, assume two matrices - >> >> A looks like: >> >> 10 10 >> 10 10 >> >> B looks like: >> >> 20 20 >> 20 20 >> >> How do I combine them such that I get alternating rows from A and B? My >> final result should be C which looks like: >> >> 10 10 >> 20 20 >> 10 10 >> 20 20 >> >> Thanks very much, and I am sorry for such a newbie question. >> >> Shruthi >> -- >> View this message in context: http://www.nabble.com/Matrix-tp21792064p21792064.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. >> >