Can anybody please tell me a good way to do the following? Given a vector, number of rows and number of columns, return a matrix as follows. Easiest to give an example: x=c(1,2), nrow=5, ncol=4 return the matrix: 1 0 0 0 2 1 0 0 0 2 1 0 0 0 2 1 0 0 0 2 Thanks very much for any help! Bill
Try this: m <- matrix(0, nrow = 5, ncol = 4) diag(m) <- x[1] diag(m[-1,]) <- x[2] On Sat, Jul 4, 2009 at 12:17 PM, William Simpson < william.a.simpson@gmail.com> wrote:> Can anybody please tell me a good way to do the following? > > Given a vector, number of rows and number of columns, return a matrix > as follows. Easiest to give an example: > > x=c(1,2), nrow=5, ncol=4 > > return the matrix: > > 1 0 0 0 > 2 1 0 0 > 0 2 1 0 > 0 0 2 1 > 0 0 0 2 > > Thanks very much for any help! > Bill > > ______________________________________________ > 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 Jul 4, 2009, at 11:17 AM, William Simpson wrote:> Can anybody please tell me a good way to do the following? > > Given a vector, number of rows and number of columns, return a matrix > as follows. Easiest to give an example: > > x=c(1,2), nrow=5, ncol=4You ought to separate those assignments with semicolons: x=c(1,2); nrow=5; ncol=4 > MM <- matrix( c(x,rep(0, nrow-length(x)+1)), nrow=nrow, ncol=ncol) Warning message: In matrix(c(x, rep(0, nrow - length(x) + 1)), nrow = nrow, ncol = ncol) : data length [6] is not a sub-multiple or multiple of the number of rows [5] > MM [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 0 2 1 0 [4,] 0 0 2 1 [5,] 0 0 0 2 OR------ > M2 <- diag( , nrow=5, ncol=4) > M2[row(M2) == col(M2)+1] <- 2 > M2 [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 0 2 1 0 [4,] 0 0 2 1 [5,] 0 0 0 2> > return the matrix: > > 1 0 0 0 > 2 1 0 0 > 0 2 1 0 > 0 0 2 1 > 0 0 0 2 > > Thanks very much for any help! > Bill > > ______________________________________________David Winsemius, MD Heritage Laboratories West Hartford, CT
On Sat, 4 Jul 2009, William Simpson wrote:> Can anybody please tell me a good way to do the following?Not sure how you want cases like x = 1:3, nrow=2 , ncol=7 to be handled, but for the example you give, this works: mat <- matrix(0,nr=nrow,nc=ncol) indx <- outer( seq( from=0, length=length(x) ), seq( from=1, by=nrow+1, length=ncol ), "+" ) mat[ indx ] <- x HTH, Chuck> > Given a vector, number of rows and number of columns, return a matrix > as follows. Easiest to give an example: > > x=c(1,2), nrow=5, ncol=4 > > return the matrix: > > 1 0 0 0 > 2 1 0 0 > 0 2 1 0 > 0 0 2 1 > 0 0 0 2 > > Thanks very much for any help! > Bill > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Thanks everyone for the help. I should have said that I want to do this generally, not as a one-off. So I want a function to do it. Like this tp<-function(x, nr, nc) { matrix( c(x,rep(0, nr-length(x)+1)), nrow=nr, ncol=nc) } tp(x=c(1,2), nr=5, nc=4) This one looks good -- the warning message is annoying though... Bill
On Jul 4, 2009, at 11:59 AM, William Simpson wrote:> Thanks everyone for the help. > > I should have said that I want to do this generally, not as a one-off. > So I want a function to do it. Like this > > tp<-function(x, nr, nc) > { > matrix( c(x,rep(0, nr-length(x)+1)), nrow=nr, ncol=nc) > } > > tp(x=c(1,2), nr=5, nc=4) > > This one looks good -- the warning message is annoying though...> tp<-function(x, nr, nc) + {suppressWarnings( + matrix( c(x,rep(0, nr-length(x)+1)), nrow=nr, ncol=nc) ) + } > > tp(x=c(1,2), nr=5, nc=4) [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 0 2 1 0 [4,] 0 0 2 1 [5,] 0 0 0 2 -- David Winsemius, MD Heritage Laboratories West Hartford, CT