Re: Re: ZigZag matrix rearrange
- To: mathgroup at smc.vnet.net
- Subject: [mg99096] Re: [mg99080] Re: [mg99055] ZigZag matrix rearrange
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Mon, 27 Apr 2009 01:14:37 -0400 (EDT)
- References: <200904250851.EAA14256@smc.vnet.net>
- Reply-to: drmajorbob at bigfoot.com
Far simpler is: Clear[m] m=Array[{##}&,{8,8}] (t1=Table[If[OddQ@k,Reverse@#,#]&@Diagonal[Reverse@m,k],{k,-7,7}])//Column {{{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8}},{{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8}},{{3,1},{3,2},{3,3},{3,4},{3,5},{3,6},{3,7},{3,8}},{{4,1},{4,2},{4,3},{4,4},{4,5},{4,6},{4,7},{4,8}},{{5,1},{5,2},{5,3},{5,4},{5,5},{5,6},{5,7},{5,8}},{{6,1},{6,2},{6,3},{6,4},{6,5},{6,6},{6,7},{6,8}},{{7,1},{7,2},{7,3},{7,4},{7,5},{7,6},{7,7},{7,8}},{{8,1},{8,2},{8,3},{8,4},{8,5},{8,6},{8,7},{8,8}}} {{1,1}} {{2,1},{1,2}} {{1,3},{2,2},{3,1}} {{4,1},{3,2},{2,3},{1,4}} {{1,5},{2,4},{3,3},{4,2},{5,1}} {{6,1},{5,2},{4,3},{3,4},{2,5},{1,6}} {{1,7},{2,6},{3,5},{4,4},{5,3},{6,2},{7,1}} {{8,1},{7,2},{6,3},{5,4},{4,5},{3,6},{2,7},{1,8}} {{2,8},{3,7},{4,6},{5,5},{6,4},{7,3},{8,2}} {{8,3},{7,4},{6,5},{5,6},{4,7},{3,8}} {{4,8},{5,7},{6,6},{7,5},{8,4}} {{8,5},{7,6},{6,7},{5,8}} {{6,8},{7,7},{8,6}} {{8,7},{7,8}} {{8,8}} or Flatten[t1,1]//InputForm {{1, 1}, {2, 1}, {1, 2}, {1, 3}, {2, 2}, {3, 1}, {4, 1}, {3, 2}, {2, 3}, {1, 4}, {1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}, {6, 1}, {5, 2}, {4, 3}, {3, 4}, {2, 5}, {1, 6}, {1, 7}, {2, 6}, {3, 5}, {4, 4}, {5, 3}, {6, 2}, {7, 1}, {8, 1}, {7, 2}, {6, 3}, {5, 4}, {4, 5}, {3, 6}, {2, 7}, {1, 8}, {2, 8}, {3, 7}, {4, 6}, {5, 5}, {6, 4}, {7, 3}, {8, 2}, {8, 3}, {7, 4}, {6, 5}, {5, 6}, {4, 7}, {3, 8}, {4, 8}, {5, 7}, {6, 6}, {7, 5}, {8, 4}, {8, 5}, {7, 6}, {6, 7}, {5, 8}, {6, 8}, {7, 7}, {8, 6}, {8, 7}, {7, 8}, {8, 8}} Bobby On Sun, 26 Apr 2009 00:39:34 -0500, DrMajorBob <btreat1 at austin.rr.com> wrote: > Elegance isn't always fast, but here's a possibility: > > Clear[next] > next[{1, 8}] = {2, 8}; > next[{8, 1}] = {7, 2}; > next[{x_, 1}] := next[{x, 1}] = If[OddQ@x, {x + 1, 1}, {x - 1, 2}] > next[{1, y_}] := next[{1, y}] = If[EvenQ@y, {1, y + 1}, {2, y - 1}] > next[{8, y_}] := next[{8, y}] = If[OddQ@y, {7, y + 1}, {8, y + 1}] > next[{x_, 8}] := next[{x, 8}] = If[EvenQ@x, {x + 1, 7}, {x + 1, 8}] > next[{x_, y_}] := > next[{x, y}] = If[OddQ[x + y], {x - 1, y + 1}, {x + 1, y - 1}] > > Clear[zigzag, t] > zigzag[t_] = t @@@ NestList[next, {1, 1}, 8*8 - 1] > > {t[1, 1], t[2, 1], t[1, 2], t[1, 3], t[2, 2], t[3, 1], t[4, 1], > t[3, 2], t[2, 3], t[1, 4], t[1, 5], t[2, 4], t[3, 3], t[4, 2], > t[5, 1], t[6, 1], t[5, 2], t[4, 3], t[3, 4], t[2, 5], t[1, 6], > t[1, 7], t[2, 6], t[3, 5], t[4, 4], t[5, 3], t[6, 2], t[7, 1], > t[8, 1], t[7, 2], t[6, 3], t[5, 4], t[4, 5], t[3, 6], t[2, 7], > t[1, 8], t[2, 8], t[3, 7], t[4, 6], t[5, 5], t[6, 4], t[7, 3], > t[8, 2], t[8, 3], t[7, 4], t[6, 5], t[5, 6], t[4, 7], t[3, 8], > t[4, 8], t[5, 7], t[6, 6], t[7, 5], t[8, 4], t[8, 5], t[7, 6], > t[6, 7], t[5, 8], t[6, 8], t[7, 7], t[8, 6], t[8, 7], t[7, 8], > t[8, 8]} > > or this: > > Clear[bounce, next] > bounce[{x_, 9}] := {x + 2, 8} > bounce[{9, y_}] := {8, y + 2} > bounce[{x_, 0}] := {x, 1} > bounce[{0, y_}] := {1, y} > bounce[{x_, y_}] := {x, y} > next[{x_, y_}] := > next[{x, y}] = bounce@If[OddQ[x + y], {x - 1, y + 1}, {x + 1, y - 1}] > > Clear[zigzag, t] > zigzag[t_] = t @@@ NestList[next, {1, 1}, 8*8 - 1] > > I've switched x and y so that x is horizontal and y is vertical. > > Bobby > > On Sat, 25 Apr 2009 03:51:18 -0500, Serych Jakub <Serych at panska.cz> > wrote: > >> Dear community, >> I'm trying to rearrange 8 x 8 matrix of numbers in ZigZag manner (as it >> is >> used in JPEG comprimation algorithm http://en.wikipedia.org/wiki/Jpeg). >> >> I have created function which does it without problems, but it does it >> "by >> hand" and it seems to me, that it could be done by some more elegant >> algorithm. >> Does somebody have any idea, how to do it more elegantly? >> >> My attempt: >> zigzag[t_] := { >> t[[1, 1]], >> t[[1, 2]], t[[2, 1]], >> t[[3, 1]], t[[2, 2]], t[[1, 3]], >> t[[1, 4]], t[[2, 3]], t[[3, 2]], t[[4, 1]], >> t[[5, 1]], t[[4, 2]], t[[3, 3]], t[[2, 4]], t[[1, 5]], >> t[[1, 6]], t[[2, 5]], t[[3, 4]], t[[4, 3]], t[[5, 2]], t[[6, 1]], >> t[[7, 1]], t[[6, 2]], t[[5, 3]], t[[4, 4]], t[[3, 5]], t[[2, 6]], >> t[[1, 7]], >> t[[1, 8]], t[[2, 7]], t[[3, 6]], t[[4, 5]], t[[5, 4]], t[[6, 3]], >> t[[7, 2]], t[[8, 1]], >> t[[8, 2]], t[[7, 3]], t[[6, 4]], t[[5, 5]], t[[4, 6]], t[[3, 7]], >> t[[2, 8]], >> t[[3, 8]], t[[4, 7]], t[[5, 6]], t[[6, 5]], t[[7, 4]], t[[8, 3]], >> t[[8, 4]], t[[7, 5]], t[[6, 6]], t[[5, 7]], t[[4, 8]], >> t[[5, 8]], t[[6, 7]], t[[7, 6]], t[[8, 5]], >> t[[8, 6]], t[[7, 7]], t[[6, 8]], >> t[[7, 8]], t[[8, 7]], >> t[[8, 8]]}; >> >> Thanks for any idea >> >> Jakub >> > > > -- DrMajorBob at bigfoot.com
- References:
- ZigZag matrix rearrange
- From: "Serych Jakub" <Serych@panska.cz>
- ZigZag matrix rearrange