Hi Does anyone know how I might pick out diagonal elements of a matrix using a vector? If I create a matrix a: a <- matrix(c(1:16), 4, byrow=TRUE) and I want to pick out the elements (1,1),(2,2),(3,3), or another arbitrary diagonal (upper or lower), is there any way I can use a vector to do this? So if I want a diagonal of size 3, I could create a vector like x <- c(0:2) and then pick out a[1+x,1+x]? Currently, using a vector in this way gives me a sub-matrix, as I get the cartesian product of the indexes (which is to be expected, as I guess I am misusing the vector notion here). Anyone know how to do this? Cheers Rory [[alternative HTML version deleted]]
On 4/12/2008 5:56 AM, Rory Winston wrote:> Hi > > Does anyone know how I might pick out diagonal elements of a matrix using a > vector? > > If I create a matrix a: > > a <- matrix(c(1:16), 4, byrow=TRUE) > > and I want to pick out the elements (1,1),(2,2),(3,3), or another arbitrary > diagonal (upper or lower), is there any way I can use a vector to do this? > So if I want a diagonal of size 3, I could create a vector like x <- c(0:2) > and then pick out a[1+x,1+x]? Currently, using a vector in this way gives me > a sub-matrix, as I get the cartesian product of the indexes (which is to be > expected, as I guess I am misusing the vector notion here). > > Anyone know how to do this?a <- matrix(c(1:16), 4, byrow=TRUE) a [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16 diag(a) [1] 1 6 11 16 diag(a[1:3,2:4]) [1] 2 7 12 ?diag> Cheers > Rory > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On Sat, 2008-04-12 at 10:56 +0100, Rory Winston wrote:> Hi > > Does anyone know how I might pick out diagonal elements of a matrix using a > vector? > > If I create a matrix a: > > a <- matrix(c(1:16), 4, byrow=TRUE)Not sure if this does all that you describe below, but: diag(a) and you can subset that to get the first 3 elements say: diag(a)[1:3] HTH G> > and I want to pick out the elements (1,1),(2,2),(3,3), or another arbitrary > diagonal (upper or lower), is there any way I can use a vector to do this? > So if I want a diagonal of size 3, I could create a vector like x <- c(0:2) > and then pick out a[1+x,1+x]? Currently, using a vector in this way gives me > a sub-matrix, as I get the cartesian product of the indexes (which is to be > expected, as I guess I am misusing the vector notion here). > > Anyone know how to do this? > > Cheers > Rory > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Rory Winston wrote:> Hi > > Does anyone know how I might pick out diagonal elements of a matrix using a > vector? > > If I create a matrix a: > > a <- matrix(c(1:16), 4, byrow=TRUE) > > and I want to pick out the elements (1,1),(2,2),(3,3), or another arbitrary > diagonal (upper or lower), is there any way I can use a vector to do this? > So if I want a diagonal of size 3, I could create a vector like x <- c(0:2) > and then pick out a[1+x,1+x]? Currently, using a vector in this way gives me > a sub-matrix, as I get the cartesian product of the indexes (which is to be > expected, as I guess I am misusing the vector notion here). > > Anyone know how to do this? >diag() is what you want, as others have said. For more general cases, you can also consider matrix indexing using two columns of indices: > diag(a) [1] 1 6 11 16 > x <- 0:2 > a[cbind(1 + x, 1 + x)] [1] 1 6 11 Duncan Murdoch> Cheers > Rory > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >