Re: Partition a list based on columns
- To: mathgroup at smc.vnet.net
- Subject: [mg119699] Re: Partition a list based on columns
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Fri, 17 Jun 2011 05:52:47 -0400 (EDT)
- References: <itcdai$d13$1@smc.vnet.net> <201106170406.AAA28684@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
How about
Clear[m, n]
m[x_List] := Module[{n = Sqrt@Length@x},
Normal@SparseArray[
Rule @@@
Transpose@{SortBy[Flatten[Array[List, {n, n}], 1], Total], x}] /;
IntegerQ@n
]
m[Range@9] // MatrixForm
(* suppressed *)
m[Range@16] // MatrixForm
(* suppressed *)
m[Range@7]
m[{1, 2, 3, 4, 5, 6, 7}]
Bobby
On Thu, 16 Jun 2011 23:06:31 -0500, Ray Koopman <koopman at sfu.ca> wrote:
> 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 is better than my previous suggestion:
>
> m[n_] := Partition[FoldList[Plus,0,Join[Range[n-1],Range[n-1,1,-1]]],
> n, 1] + Range[n]
>
--
DrMajorBob at yahoo.com
- References:
- Re: Partition a list based on columns
- From: Ray Koopman <koopman@sfu.ca>
- Re: Partition a list based on columns