[R] calculations on diagonals of a matrix
Alec Stephenson
a.stephenson at lancaster.ac.uk
Sat May 19 23:36:21 CEST 2001
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 at 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 at 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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list