[R] Efficent way to create an nxn upper triangular matrix of one's

Dale Steele dale.w.steele at gmail.com
Wed Feb 11 22:22:51 CET 2009


The code below create an nxn upper triangular matrix of one's.  I'm
stuck on finding a more efficient vectorized way - Thanks.  --Dale

n <- 9
data <- matrix(data=NA, nrow=n, ncol=n)
data
for (i in 1:n) {
    data[,i] <- c(rep(1,i), rep(0,n-i))
}
data




More information about the R-help mailing list