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