Dear all, seems that easy question but cannot find the function for that. How to get the elements of the minor diagonal of the matrix? Thanks a lot. [[alternative HTML version deleted]]
This this what you want?> A=matrix(1:16,ncol=4) > A[,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> diag(A[1:4,4:1])[1] 13 10 7 4>-- View this message in context: http://r.789695.n4.nabble.com/minor-diagonal-in-R-tp2529676p2529710.html Sent from the R help mailing list archive at Nabble.com.
On Tue, 7 Sep 2010 06:26:09 -0700 (PDT) "Peng, C" <cpeng.usm at gmail.com> wrote:> > This this what you want? > > > A=matrix(1:16,ncol=4) > > A > [,1] [,2] [,3] [,4] > [1,] 1 5 9 13 > [2,] 2 6 10 14 > [3,] 3 7 11 15 > [4,] 4 8 12 16 > > diag(A[1:4,4:1]) > [1] 13 10 7 4Or> A[cbind(1:4,4:1)][1] 13 10 7 4 one character more to type, but could be more efficient for large A :) Cheers, Berwin ========================== Full address ===========================Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Maths and Stats (M019) +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin
On Tue, Sep 7, 2010 at 9:09 AM, Trafim Vanishek <rdapamoga at gmail.com> wrote:> Dear all, > > seems that easy question but cannot find the function for that. > How to get the elements of the minor diagonal of the matrix? >Here are a few possibilities: # test matrix x <- matrix(1:16, 4) # 1 diag(x[, nrow(x):1]) # 2 rev(x[row(x) + col(x) == nrow(x) + 1]) # 3 x[to = seq(to = nrow(x), by = 1 - nrow(x), length = nrow(x))] # 4 diag(as.matrix(rev(as.data.frame(x)))) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com