[S] Q: How to sum the diagonals of a matrix?

david.h.pattison@ssa.gov
Thu, 19 Mar 98 08:06:15 -0500


Is there an easy (i.e., non-looping) way to sum the diagonals of a
matrix? In my case I want the diagonals running from lower left to
upper right defined by a constant element index sum (e.g., all the
elements A[i,j] with i+j=2, all with i+j=3, etc.). In an r by c
matrix there will be r+c-1 such diagonals, with indices summing to 2,
3, ... r+c. In the example matrix below, I have given equal values
--the index sums-- to the diagonals whose sums I want:

2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11.

The answer I am looking for, in this case, is the 10-vector

(2 6 12 20 24 28 32 27 20 11).

I hope this is not _too_ clear. If you respond to me, I will
summarize to the list.

Thanks,

David Pattison
David.H.Pattison@ssa.gov
Social Security Administration

202-358-6240

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news