I'm trying to create a diagonal matrix with 390 rows and 2340 columns, but in the diagonal I need to have a vector of 1, rep(1,6)
.
For example, these should be the first two rows:
1111110.............................0
0000001111110.......................0
How can I do it?
Thinking of it row by row, you want six ones, followed by 2340 zeros (six of which overflow into the next row, shifting the sequence of ones by six columns), repeated over and over again. So you can acheive this by doing:
matrix(c(rep(1, 6), rep(0, 2340)), ncol = 2340, nrow = 390, byrow = TRUE)
Note that there will be a warning about the data not being a multiple of the number of rows, but that's expected: the zeros on the last row would be assigned to row 391, but we say we only want 390, so the data gets truncated.
You can verify the result with:
x[1:20, 1:20] # Top-left corner
x[371:390, 2321:2340] # Bottom-right corner