Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Extract any diagonal from a square matrix...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66285] Re: [mg66279] Extract any diagonal from a square matrix...
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Mon, 8 May 2006 00:45:59 -0400 (EDT)
  • References: <200605070350.XAA08508@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 7 May 2006, at 12:50, 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.
> 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
>


Here is another approach, quite different form everything you mention  
above, and I think quite elegant, but unfortunately not very  
efficient. (I have not tested it but in general algebraic approaches  
of this kind, tend to be fairly inefficient).
I will use somewhat different notation form yours, with d=0  
corresponding to the main diagonal, which this seems more natural to  
me. One can make it all work the way you want of course.


DiagonalT[a_?MatrixQ, d_Integer] := Array[If[#2 - #1 == d, 1, 0] &,  
Dimensions[a]]*a

e.g.


m = Array[a, {4, 4}]

{{a[1, 1], a[1, 2], a[1, 3], a[1, 4]},
   {a[2, 1], a[2, 2], a[2, 3], a[2, 4]},
   {a[3, 1], a[3, 2], a[3, 3], a[3, 4]},
   {a[4, 1], a[4, 2], a[4, 3], a[4, 4]}}


DiagonalT[m, 1]

{{0, a[1, 2], 0, 0}, {0, 0, a[2, 3], 0},
   {0, 0, 0, a[3, 4]}, {0, 0, 0, 0}}

Andrzej Kozlowski
Tokyo, Japan


  • Prev by Date: Re: Extract any diagonal from a square matrix...
  • Next by Date: Re: 3D surface plotting
  • Previous by thread: Re: Extract any diagonal from a square matrix...
  • Next by thread: Re: Extract any diagonal from a square matrix...