MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Extract any diagonal from a square matrix...


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.
Essentially, the function constructs a submatrix of the matrix so that
the requested diagonal from the matrix becomes the main diagonal of the
submatrix, which is then retrieved.  D is the diagonal to retrieve,
where d =

3 -- 2nd superdiagonal
2 -- 1st superdiagonal
1 --  main diagonal
-2 -- 1st subdiagonal
-3 -- 2nd subdiagonal

etc...

Some other things I've considered --

...rotating the elements of each row until column 1 becomes the
requested diagonal;
...dropping elements from each row until the first or last element in
each row becomes the next element in the requested diagonal;
...flattening the matrix and then using Range and Part to retrieve the
requested diagonal.

Any thoughts...?

h


  • Prev by Date: Re: Re: Mathematica crashes GNOME 2.12
  • Next by Date: General--Differential equation problem..
  • Previous by thread: Re: Re: Mathematica crashes GNOME 2.12
  • Next by thread: Re: Extract any diagonal from a square matrix...