RE: Rolling up some integers

• To: mathgroup at smc.vnet.net
• Subject: [mg33432] RE: [mg33404] Rolling up some integers
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Thu, 21 Mar 2002 09:27:12 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```> -----Original Message-----
> From: DIAMOND Mark R. [mailto:dot at dot.dot]
To: mathgroup at smc.vnet.net
> Sent: Wednesday, March 20, 2002 7:53 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg33432] [mg33404] Rolling up some integers
>
>
> Can anyone see an easy way of  putting a fiven list of n^2
> integers into an
> n*n matrix along the diagonals---could be either not-snaking
> (preferred)
> snaking. The *ordering*, but not necessarily the integers
> themselves, would
> be
>
> 1 3 6
> 2 5 8
> 4 7 9
>
> I would call this non-snaking, or  the following, snaking.
>
> 1 2 6
> 3 5 7
> 4 8 9
>
>
> It occurred to me that this might be possible by first
> partitioning into
> ascending and descending size lists representing each
> diagonal, but aside
> from constructing an explicit secondary matrix of indices, I can't see
> another way of doing it.
>
> Cheers,
>
> Mark
> --
> Mark R. Diamond
> No spam email ROT13: znexq at cfl.hjn.rqh.nh
> No crawler web page ROT13 uggc://jjj.cfl.hjn.rqh.nh/hfre/znexq
>
>
>
>
Mark,

generate a matrix of indices to cast your data into the appropriate form.
Define

n=7;

hatchx=
With[{zzz = Rest[FoldList[Plus,0,Range[2*n]]]-
Join[Table[0,{n}], Rest[FoldList[Plus,0,2*Range[n]-1]]]},
(Take[zzz,{1,n}+#]-#&)/@Range[0,n-1]]

{{ 1, 3, 6,10,15,21,28},
{ 2, 5, 9,14,20,27,34},
{ 4, 8,13,19,26,33,39},
{ 7,12,18,25,32,38,43},
{11,17,24,31,37,42,46},
{16,23,30,36,41,45,48},
{22,29,35,40,44,47,49}}

Then bring your list of data, e.g.

m=Array[a,{n^2}];

(m[[#]]&)/@hatchx //MatrixForm

to the "hatched" form.

If you prefer the snakes, generate different indices:

(snakex =
MapIndexed[
If[(-1)^(Plus @@ #2) === 1, #1,
hatchx[[Sequence@@Reverse[#2] ]] ] &,
hatchx, {2}]) // MatrixForm

A different, more straightforward variant based on list processing would be:

hatchx = Module[
{r = Rest[FoldList[{#1[[2]]+1,#1[[2]]+#2}&,{0,0},Range[n]]],
rr, rawx},
rr = -Reverse /@ Rest[Reverse[r]];

rawx = Join[

rawx[[#1 - 1 + Range[n],-#1]]& /@ Range[n]
]

snakex = Module[
{r = Rest[FoldList[{#1[[2]]+1,#1[[2]]+#2}&,{0, 0},Range[n]]],
rr, rawx},
rr = (-1)*Reverse /@ Rest[Reverse[r]];

rawx = Join[
If[(-1)^(#2[[1]]-1) === 1, Identity, Reverse][Take[Range[n^2],
#1]],
n] &, r],
If[(-1)^#2[[1]] === 1, Identity, Reverse][Take[Range[n^2], #1]],
n] &, rr]];
rawx[[#1 - 1 + Range[n], -#1]]& /@ Range[n]
]

--
Hartmut

```

• Prev by Date: Re: Re: Full text of the current warning message(s)
• Next by Date: RE: Value and position in list
• Previous by thread: Re: Rolling up some integers
• Next by thread: Question on FindMinimum