简体   繁体   中英

Creating a non-symmetric matrix of 1s with non-symmetric diagonal of 0s in R

EDITED:

I would like to efficiently create a 1452 x 44 matrix, where:

  • the first column has 33 zeroes followed by 1419 ones
  • the second column has 33 ones, then 33 zeroes, then 1386 ones ...
  • the thirty third column as 1419 ones followed by 33 zeroes

How can I do this in R? Many thanks in advance!

After edits, moving my comment to an answer:

x1 = 44
x2 = 1452
m = matrix(c(rep(rep(0:1, c(x1, x2)), x2 / x1 - 1), rep(0, x1)),
           ncol = x2 / x1) 

Essentially, you want a pattern of 44 0s then 1452 1s repeated a bunch. We stick on the last set of 44 0s so it ends on 0 and set the dimensions accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM