[R] minor diagonal in R

Berwin A Turlach berwin at maths.uwa.edu.au
Tue Sep 7 15:37:26 CEST 2010


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  4

Or 

> 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



More information about the R-help mailing list