Re: Partition a list based on columns
- To: mathgroup at smc.vnet.net
- Subject: [mg119664] Re: Partition a list based on columns
- From: Ray Koopman <koopman at sfu.ca>
- Date: Thu, 16 Jun 2011 06:23:23 -0400 (EDT)
- References: <itcdai$d13$1@smc.vnet.net>
On Jun 16, 1:02 am, StatsMath <stats.ma... at gmail.com> wrote:
> [...]
>
> What would be a good way to line up elements in a list in a diagonal
> fashion, for ex: from Range[9] want to create the follwing matrix:
>
> 1 2 4
> 3 5 7
> 6 8 9
This will give a square matrix of order n whose elements are
the integers 1...n^2 in the positions you asked for:
m[n_] := With[{b=FoldList[Plus,0,Join[Range[n-1],Range[n-1,1,-1]]]},
Table[Take[b,{i,i+n-1}]+i,{i,n}]]
m[5]
{{ 1, 2, 4, 7, 11},
{ 3, 5, 8, 12, 16},
{ 6, 9, 13, 17, 20},
{10, 14, 18, 21, 23},
{15, 19, 22, 24, 25}}