Hello R-users, I have a symmetric matrix of numerical values and I want to obtain those values in the upper or lower triangle of the matrix in a vector. I tried to do the job by using two for-loops but it doens't seem to be a clever way, and I'd like to know a more efficient code for a large matrix of thousands of rows and columns. Below is my code for your reference. Thanks a lot. John #################### # mtx.sym is a symmetric matrix> my.ftn <- function(size_mtx, mtx) {+ my.vector <- c() + for ( i in 1:size_mtx ) { + cat(".") + for ( j in 1:size_mtx ) { + if ( upper.tri(mtx)[i,j] ) { + my.vector <- c(my.vector, mtx[i,j]) + }}} + cat("\n") + }># if I have a matrix, mtx.sym, of 100x100> my.ftn(100, mtx.sym)
lower triangle can be obtained by A[row(A)>col(A)] url: www.econ.uiuc.edu/~roger Roger Koenker email rkoenker at uiuc.edu Department of Economics vox: 217-333-4558 University of Illinois fax: 217-244-6678 Champaign, IL 61820 On Nov 25, 2004, at 11:15 AM, John wrote:> Hello R-users, > > I have a symmetric matrix of numerical values and I > want to obtain those values in the upper or lower > triangle of the matrix in a vector. I tried to do the > job by using two for-loops but it doens't seem to be a > clever way, and I'd like to know a more efficient code > for a large matrix of thousands of rows and columns. > Below is my code for your reference. > > Thanks a lot. > > John > > #################### > > # mtx.sym is a symmetric matrix > >> my.ftn <- function(size_mtx, mtx) { > + my.vector <- c() > + for ( i in 1:size_mtx ) { > + cat(".") > + for ( j in 1:size_mtx ) { > + if ( upper.tri(mtx)[i,j] ) { > + my.vector <- c(my.vector, mtx[i,j]) > + }}} > + cat("\n") > + } >> > # if I have a matrix, mtx.sym, of 100x100 >> my.ftn(100, mtx.sym) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
John wrote:> Hello R-users, > > I have a symmetric matrix of numerical values and I > want to obtain those values in the upper or lower > triangle of the matrix in a vector. I tried to do the > job by using two for-loops but it doens't seem to be a > clever way, and I'd like to know a more efficient code > for a large matrix of thousands of rows and columns. > Below is my code for your reference.See ?upper.tri Uwe Ligges> Thanks a lot. > > John > > #################### > > # mtx.sym is a symmetric matrix > > >>my.ftn <- function(size_mtx, mtx) { > > + my.vector <- c() > + for ( i in 1:size_mtx ) { > + cat(".") > + for ( j in 1:size_mtx ) { > + if ( upper.tri(mtx)[i,j] ) { > + my.vector <- c(my.vector, mtx[i,j]) > + }}} > + cat("\n") > + } > > # if I have a matrix, mtx.sym, of 100x100 > >>my.ftn(100, mtx.sym) > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Thursday 25 November 2004 11:15, John wrote:> Hello R-users, > > I have a symmetric matrix of numerical values and I > want to obtain those values in the upper or lower > triangle of the matrix in a vector. I tried to do the > job by using two for-loops but it doens't seem to be a > clever way, and I'd like to know a more efficient code > for a large matrix of thousands of rows and columns. > Below is my code for your reference.Try mtx[lower.tri(mtx)] which should give you the same order as your code (untested). HTH, Deepayan> > Thanks a lot. > > John > > #################### > > # mtx.sym is a symmetric matrix > > > my.ftn <- function(size_mtx, mtx) { > > + my.vector <- c() > + for ( i in 1:size_mtx ) { > + cat(".") > + for ( j in 1:size_mtx ) { > + if ( upper.tri(mtx)[i,j] ) { > + my.vector <- c(my.vector, mtx[i,j]) > + }}} > + cat("\n") > + } > > # if I have a matrix, mtx.sym, of 100x100 > > > my.ftn(100, mtx.sym)
On 25-Nov-04 John wrote:> Hello R-users, > > I have a symmetric matrix of numerical values and I > want to obtain those values in the upper or lower > triangle of the matrix in a vector. I tried to do the > job by using two for-loops but it doens't seem to be a > clever way, and I'd like to know a more efficient code > for a large matrix of thousands of rows and columns.The two functions uuper.tri and lower.tri do just this job! Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 25-Nov-04 Time: 18:54:20 ------------------------------ XFMail ------------------------------