Re: [R] calculations on diagonals of a matrix

About this list Date view Thread view Subject view Author view

From: Alec Stephenson (a.stephenson@lancaster.ac.uk)
Date: Sun 20 May 2001 - 07:36:21 EST


Message-ID: <Pine.GSO.4.21.0105192226420.188-100000@unixc.lancs.ac.uk>


you could index the elements explicitly via

index.matrix <-
matrix(seq(to=i,len=min(i,j)),seq(to=j,len=min(i,j)),ncol=2)

and so b_ij = sum(a.matrix[index.matrix]), or you could use the function
diag on the appropriate sub matrices of a.matrix.

Alec Stephenson tel +44 (0) 1524 593950
Department of Mathematics and Statistics fax +44 (0) 1524 592681
Lancaster University email a.stephenson@lancaster.ac.uk
Lancaster LA1 4YF

On Sat, 19 May 2001, Griffith Feeney wrote:

> Given an nxm matrix A I want to compute the nxm matrix B whose ij-th
> element is the sum of the elements of A lying on the diagonal that ends
> with element ij, i.e.,
>
> b_ij = a_ij + a_(i-1)(j-1) + a_(i-2)(j-2) + ...
>
> In APL (which I no longer use), I would use the 'rotate' operator to derive
> an array whose columns are diagonals of the given array and then cumulate
> down columns. Is there a similar operator in R, or is there another/better way?
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2b30 : Fri 22 Jun 2001 - 18:58:42 EST