Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #3126 > unrolled thread
| Started by | StatsMath <stats.math8@gmail.com> |
|---|---|
| First post | 2011-06-16 08:02 +0000 |
| Last post | 2011-06-17 09:52 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Partition a list based on columns StatsMath <stats.math8@gmail.com> - 2011-06-16 08:02 +0000
Re: Partition a list based on columns Ray Koopman <koopman@sfu.ca> - 2011-06-16 10:24 +0000
Re: Partition a list based on columns Ray Koopman <koopman@sfu.ca> - 2011-06-17 04:09 +0000
Re: Partition a list based on columns Ray Koopman <koopman@sfu.ca> - 2011-06-17 04:06 +0000
Re: Partition a list based on columns DrMajorBob <btreat1@austin.rr.com> - 2011-06-17 09:52 +0000
| From | StatsMath <stats.math8@gmail.com> |
|---|---|
| Date | 2011-06-16 08:02 +0000 |
| Subject | Partition a list based on columns |
| Message-ID | <itcdai$d13$1@smc.vnet.net> |
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!
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-06-16 10:24 +0000 |
| Message-ID | <itclkg$gtq$1@smc.vnet.net> |
| In reply to | #3126 |
On Jun 16, 1:02 am, StatsMath <stats.ma...@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}}
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-06-17 04:09 +0000 |
| Message-ID | <itek2d$s5q$1@smc.vnet.net> |
| In reply to | #3126 |
On Jun 16, 1:02 am, StatsMath <stats.ma...@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
>
> Thanks in advance!
This will give an r x c matrix whose elements are
the integers 1...r*c in the positions you asked for:
m[r_,c_] := With[{d = Which[
r < c-1, Join[Range[ r ],Table[r,{c-2-r}],Range[ r ,1,-1]],
r > c-1, Join[Range[c-1],Table[c-1,{r-c}],Range[c-1,1,-1]],
True , Join[Range[c-2], Range[c-1,1,-1]]]},
Partition[FoldList[Plus,0,d],c,1] + Range@r]
m[3,5] //TableForm
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-06-17 04:06 +0000 |
| Message-ID | <itejse$s0k$1@smc.vnet.net> |
| In reply to | #3126 |
On Jun 16, 1:02 am, StatsMath <stats.ma...@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]
[toc] | [prev] | [next] | [standalone]
| From | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| Date | 2011-06-17 09:52 +0000 |
| Message-ID | <itf859$239$1@smc.vnet.net> |
| In reply to | #3126 |
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@sfu.ca> wrote:
> On Jun 16, 1:02 am, StatsMath <stats.ma...@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@yahoo.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web