[R] triangular matrices input/output
Hi, Is there any package that deals with triangular matrices? Say ways of inputting an upper (lower) triangular matrix? Or convert a vector of length 6 to an upper (lower) triangular matrix (by row/column)? Thanks! - ## PhD candidate in Statistics Big R Fan Big LEGO Fan Big sTaTs Fan ## -- View this message in context: http://r.789695.n4.nabble.com/triangular-matrices-input-output-tp4630310.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] triangular matrices input/output
The Matrix package provides good support for many special sorts of matrices, but here it looks like you probably don't need that additional machinery for such small case: makeUpper - function(vec, diag = FALSE){ n - (-1 + sqrt(1 + 8*length(vec)))/2 stopifnot(isTRUE(all.equal(n, as.integer(n if(!diag) n - n + 1 mat - matrix(0, ncol = n, nrow = n) mat[upper.tri(mat, diag)] - vec mat } I think does what you want and it's not too hard to generalize to lower triangular. E.g., v - 1:6 makeUpper(v) makeUpper(v, diag = TRUE) It's not super well tested though so caveat lector. Michael On Wed, May 16, 2012 at 5:09 PM, casperyc caspe...@hotmail.co.uk wrote: Hi, Is there any package that deals with triangular matrices? Say ways of inputting an upper (lower) triangular matrix? Or convert a vector of length 6 to an upper (lower) triangular matrix (by row/column)? Thanks! - ## PhD candidate in Statistics Big R Fan Big LEGO Fan Big sTaTs Fan ## -- View this message in context: http://r.789695.n4.nabble.com/triangular-matrices-input-output-tp4630310.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] triangular matrices input/output
Do leave the posts for anyone else who might google the same question. (I don't think you really could delete the post anyways, perhaps only on one mirror) You could probably use some combination or rev() and t() to fill by row, but I haven't thought through the geometry all the way yet. Michael On May 16, 2012, at 8:13 PM, YUProf caspe...@hotmail.co.uk wrote: Hi Michael, I have figured out a 'super' easy way myself and already deleted the post. It can be done using: (no package necssary) d=c(3,6,2,1,4,5) x=matrix(,3,3) # by column, x[!lower.tri(x)]=d I am still trying very hard to think of a way to fit it by row as I sometimes have to! THANKS! Chen == Mr Chen YU PhD candidate in Statistics School of Mathematics, Statistics and Actuarial Science, University of Kent D7/D Woolf College, The Pavilion, Giles Lane, Canterbury, Kent CT2 7BQ Mobile: +44(0)7725003559 == From: michael.weyla...@gmail.com Date: Wed, 16 May 2012 19:41:36 -0400 Subject: Re: [R] triangular matrices input/output To: caspe...@hotmail.co.uk CC: r-help@r-project.org The Matrix package provides good support for many special sorts of matrices, but here it looks like you probably don't need that additional machinery for such small case: makeUpper - function(vec, diag = FALSE){ n - (-1 + sqrt(1 + 8*length(vec)))/2 stopifnot(isTRUE(all.equal(n, as.integer(n if(!diag) n - n + 1 mat - matrix(0, ncol = n, nrow = n) mat[upper.tri(mat, diag)] - vec mat } I think does what you want and it's not too hard to generalize to lower triangular. E.g., v - 1:6 makeUpper(v) makeUpper(v, diag = TRUE) It's not super well tested though so caveat lector. Michael On Wed, May 16, 2012 at 5:09 PM, casperyc caspe...@hotmail.co.uk wrote: Hi, Is there any package that deals with triangular matrices? Say ways of inputting an upper (lower) triangular matrix? Or convert a vector of length 6 to an upper (lower) triangular matrix (by row/column)? Thanks! - ## PhD candidate in Statistics Big R Fan Big LEGO Fan Big sTaTs Fan ## -- View this message in context: http://r.789695.n4.nabble.com/triangular-matrices-input-output-tp4630310.html Sent from the R help mailing list archive at Nabble.com. __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] triangular matrices input/output
Hello, I needed this once. upper.diag - function(x, byrow=FALSE){ m - sqrt(1 + 8*length(x)) if(abs(m - floor(m)) .Machine$double.eps^0.5) m - (m - 1)/2 else{ warning(length of 'x' is not a triangular number.) m - floor((m - 1)/2) } y - matrix(0, nrow=m, ncol=m) if(byrow){ y[lower.tri(y, TRUE)] - x y - t(y) }else y[upper.tri(y, TRUE)] - x y } lower.diag - function(x, byrow=FALSE){ m - sqrt(1 + 8*length(x)) if(abs(m - floor(m)) .Machine$double.eps^0.5) m - (m - 1)/2 else{ warning(length of 'x' is not a triangular number.) m - floor((m - 1)/2) } y - matrix(0, nrow=m, ncol=m) if(byrow){ y[upper.tri(y, TRUE)] - x y - t(y) }else y[lower.tri(y, TRUE)] - x y } lower.diag(1:6) lower.diag(1:10, TRUE) lower.diag(1:8) upper.diag(1:6) upper.diag(1:10, TRUE) upper.diag(1:12) Hope this helps, Rui Barradas casperyc wrote Hi, Is there any package that deals with triangular matrices? Say ways of inputting an upper (lower) triangular matrix? Or convert a vector of length 6 to an upper (lower) triangular matrix (by row/column)? Thanks! - ## PhD candidate in Statistics Big R Fan Big LEGO Fan Big sTaTs Fan ## -- View this message in context: http://r.789695.n4.nabble.com/triangular-matrices-input-output-tp4630310.html Sent from the R help mailing list archive at Nabble.com. __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/triangular-matrices-input-output-tp4630322p4630328.html Sent from the R help mailing list archive at Nabble.com. __ 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.