how insert zeros in seq in R

Viewed 558

I need get this:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    0    2    0    3    0    4    0    5
[2,]    0    0    0    0    0    0    0    0    0
[3,]    6    0    7    0    8    0    9    0   10
[4,]    0    0    0    0    0    0    0    0    0
[5,]   11    0   12    0   13    0   14    0   15
[6,]    0    0    0    0    0    0    0    0    0
[7,]   16    0   17    0   18    0   19    0   20
[8,]    0    0    0    0    0    0    0    0    0
[9,]   21    0   22    0   23    0   24    0   25

I write:

  n<-5
  x <- c(1,0,2,0,3,0,4,0,5,0,0,0,0,0,0,0,0,0,6,0,7,0,8,0,9,0,10,0,0,0,0,0,0,0,0,0,11,0,12,0,13,0,14,0,15,0,0,0,0,0,0,0,0,0,16,0,17,0,18,0,19,0,20,0,0,0,0,0,0,0,0,0,21,0,22,0,23,0,24,0,25)
  x
  M<-matrix(x,ncol=n+(n-1),byrow=TRUE)
  length(x)
  M

So, can I get this using n ? for example: x <- 1:n^2 ?? That is, I want to have seq(n^2) in a matrix, but I need rows with zeros and columns with zeros?

I hope my question is understandable, thank you very much :)

4 Answers
Related