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
Efficent way to create an nxn upper triangular matrix of one's
5 messages · Dale Steele, Sundar Dorai-Raj, Jorge Ivan Velez +2 more
Try x <- diag(n) x[upper.tri(x)] <- 1
On Wed, Feb 11, 2009 at 1:22 PM, Dale Steele <dale.w.steele at gmail.com> wrote:
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 at 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090211/3d8cd85c/attachment-0001.pl>
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-bounces at r-project.org
[mailto:r-help-bounces at 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 at 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.
1 day later
The other solutions offered are perfectly workable. Here is a strategy that is generalizable to other matrix designs (and on checking the source of upper.tri and lower.tri, it's not surprising that they use precisely the same strategy): n <- 9 dm <- matrix(0, nrow=n, ncol=n) dm[col(dm) >= row(dm)] <- 1 If you wanted only upper off-diagonal -1's, for instance, you could use instead: dm[col(dm) == (row(dm)+1) ] <- -1
David Winsemius
On Feb 11, 2009, at 4:22 PM, Dale Steele wrote:
> 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 at 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.