43
4
As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction:
Your task is to take a matrix (not necessarily square) and return it in unrolled form. As an example:
[1 2 3 4
5 6 7 8
9 1 2 3]
should yield
[1, 2, 5, 9, 6, 3, 4, 7, 1, 2, 8, 3]
Rules
You may assume that the matrix elements are positive integers less than 10
.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.
The input matrix may be given in any convenient, unambiguous, nested list or string format, or as a flat list along with both matrix dimensions. (Or, of course, as a matrix type if your language has those.)
The output vector may be in any convenient, unambiguous, flat list or string format.
Standard code-golf rules apply.
Test Cases
[[1]] => [1]
[[1 2] [3 1]] => [1 2 3 1]
[[1 2 3 1]] => [1 2 3 1]
[[1 2 3] [5 6 4] [9 7 8] [1 2 3]] => [1 2 5 9 6 3 4 7 1 2 8 3]
[[1 2 3 4] [5 6 7 8] [9 1 2 3]] => [1 2 5 9 6 3 4 7 1 2 8 3]
[[1 2 6 3 1 2] [5 9 4 7 8 3]] => [1 2 5 9 6 3 4 7 1 2 8 3]
[[1 2 5 9 6 3 4 7 1 2 8 3]] => [1 2 5 9 6 3 4 7 1 2 8 3]
[[1] [2] [5] [9] [6] [3] [4] [7] [1] [2] [8] [3]] => [1 2 5 9 6 3 4 7 1 2 8 3]
Related Challenges
- Reconstruct a zigzagified matrix (the somewhat trickier inverse transformation)
- Rotate the anti-diagonals
1Can the input be an actual matrix in J? Or would it need to be turned from nested lists into a matrix as part of the function? – Gareth – 2016-03-15T19:08:24.363
4If we take the matrix as a 2D array, may we still take the dimensions as inputs? – xnor – 2016-03-15T19:23:37.623
1@Gareth yes you can take a matrix type as input. – Martin Ender – 2016-03-15T19:46:35.290
1@xnor Hmmm, that one's a bit trickier. I feel like taking that amount of redundant information goes a bit into preprocessing the input. – Martin Ender – 2016-03-15T19:47:30.103
Can the flat list be in column-major order if that's the language's native order?
– Luis Mendo – 2016-03-15T20:21:46.877@DonMuesli sure. – Martin Ender – 2016-03-15T20:22:37.497
This link to a question I posted on StackOverflow in the past may also be fruitful: http://stackoverflow.com/questions/28369142/what-is-the-most-efficient-way-to-implement-zig-zag-ordering-in-matlab. It was unfortunately linked to another duplicate, but both the question and the duplicate may provide fruitful information on why the zigzag order is important in JPEG compression.
– rayryeng - Reinstate Monica – 2016-03-15T21:44:22.037This challenge is related too
– Erwan – 2016-03-16T07:08:12.823Are elements strictly positive or can we have some zero ? – Erwan – 2016-03-16T07:18:26.917
@Erwan strictly positive – Martin Ender – 2016-03-16T08:06:19.400
Shouldn't this be "unzigzagify a matrix"? This is making the input less complex then the output... – Ashwin Gupta – 2016-03-16T13:51:44.887