Here is a way to do this: round(upper.tri(matrix(1, 9, 9)))
Or if you also need the diagonal of one's round(upper.tri(matrix(1, 9, 9), diag = TRUE)) -Christos > -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Dale Steele > Sent: Wednesday, February 11, 2009 4:23 PM > To: R help > Subject: [R] Efficent way to create an nxn upper triangular > matrix of one's > > 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 > > ______________________________________________ > 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. > > ______________________________________________ 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.