Re: Partition a list based on columns
- To: mathgroup at smc.vnet.net
- Subject: [mg119681] Re: Partition a list based on columns
- From: Heike Gramberg <heike.gramberg at gmail.com>
- Date: Fri, 17 Jun 2011 00:09:24 -0400 (EDT)
- References: <201106160802.EAA13329@smc.vnet.net>
For the first part of your question you could use
Transpose[Partition[Sort[Flatten[{{3,1},{2,4}}]]],2]]
For the second part you could do something like
diag[list_, k_, l_] :=
With[{order = Flatten[Table[{i, j - i + 1}, {j, 1, k + l - 1}, {i, Max[j + 1 - l, 1], Min[j, k]}], 1]},
ReplacePart[ConstantArray[0, {k, l}], Thread[order -> PadRight[list, k l]]]]
then diag[list,k,l] would order list into a k-by-l matrix in a diagonal way
(truncating or extending list if necessary), so for example
diag[Range[12], 3, 4]
gives
{{1, 2, 4, 7}, {3, 5, 8, 10}, {6, 9, 11, 12}}
Heike.
On 16 Jun 2011, at 09:02, StatsMath wrote:
> Hi All,
>
> I am looking for help in partition a list based on columns. For ex,
> given a list {{3,1},{2,4}}, I want to sort it and then partition it
> along columns.
>
> So for the given example, I want the answer to be, {{1,3},{2,4}}.
>
> Right now If I try, Partition[Sort[Flatten[{{3,1},{2,4}}]]],2], I get
> {{1,2},{3,4}}.
>
> Looking at doc for Partition[], it doesn't seem to support aligning
> rows on columns. I can do it in a For[] loop, but that seems un-
> mathematica like, is there a good functional way to do this??
>
> ------------
>
> 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
>
> Thanks in advance!
>
- References:
- Partition a list based on columns
- From: StatsMath <stats.math8@gmail.com>
- Partition a list based on columns