Re: ZigZag matrix rearrange
- To: mathgroup at smc.vnet.net
- Subject: [mg99085] Re: [mg99055] ZigZag matrix rearrange
- From: danl at wolfram.com
- Date: Sun, 26 Apr 2009 01:40:29 -0400 (EDT)
- References: <200904250851.EAA14256@smc.vnet.net>
> 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 Can be done algorithmically using Reverse and Diagonal. mat = Array[t, {8, 8}]; Table[If[EvenQ[j], Identity, Reverse][ Diagonal[Reverse[mat, 2], j]], {j, Length[mat] - 1, -Length[mat] + 1, -1}] {{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]}} Daniel Lichtblau Wolfram Research
- References:
- ZigZag matrix rearrange
- From: "Serych Jakub" <Serych@panska.cz>
- ZigZag matrix rearrange