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

2009-02-13 Thread David Winsemius
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) >=

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

2009-02-11 Thread Christos Hatzis
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 efficie

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

2009-02-11 Thread Jorge Ivan Velez
Dear Dale, Here is one way, probably not the best: n<-9 temp<-matrix(1,ncol=n,nrow=n) temp[lower.tri(temp)] <- 0 temp HTH, Jorge On Wed, 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 vector

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

2009-02-11 Thread Sundar Dorai-Raj
Try x <- diag(n) x[upper.tri(x)] <- 1 On Wed, Feb 11, 2009 at 1: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 (

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

2009-02-11 Thread Dale Steele
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 __