Re: Extract any diagonal from a square matrix...
- To: mathgroup at smc.vnet.net
- Subject: [mg66296] Re: Extract any diagonal from a square matrix...
- From: "J Siehler" <jsiehler at gmail.com>
- Date: Mon, 8 May 2006 00:46:32 -0400 (EDT)
- References: <e3js8r$8kh$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
This is maybe a little cleaner: diag[t_, d_] := Extract[t, {#, # + d} & /@ Range[1 - Min[0, d], Length[t] - Max[0, d]]] It treats the main diagonal as d=0; 1st superdiagonal as d=1; 1st subdiagonal as -1; and so on. hawkmoon269 wrote: > Trying to put a function together that extracts any one of the > diagonals from a square matrix. Right now I have this -- > > DiagonalT[a_List, d_Integer] := > Tr[Take[Which[Positive[d], a, Negative[d], Transpose[a]], > {1, Length[a] - Abs[d] + 1}, {Abs[d], Length[a]}], List] > > This works, but I thought there might be something less cumbersome.