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
-
1\$\begingroup\$ Can 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? \$\endgroup\$Gareth– Gareth2016年03月15日 19:08:24 +00:00Commented Mar 15, 2016 at 19:08
-
4\$\begingroup\$ If we take the matrix as a 2D array, may we still take the dimensions as inputs? \$\endgroup\$xnor– xnor2016年03月15日 19:23:37 +00:00Commented Mar 15, 2016 at 19:23
-
1\$\begingroup\$ @Gareth yes you can take a matrix type as input. \$\endgroup\$Martin Ender– Martin Ender2016年03月15日 19:46:35 +00:00Commented Mar 15, 2016 at 19:46
-
1\$\begingroup\$ @xnor Hmmm, that one's a bit trickier. I feel like taking that amount of redundant information goes a bit into preprocessing the input. \$\endgroup\$Martin Ender– Martin Ender2016年03月15日 19:47:30 +00:00Commented Mar 15, 2016 at 19:47
-
\$\begingroup\$ Can the flat list be in column-major order if that's the language's native order? \$\endgroup\$Luis Mendo– Luis Mendo2016年03月15日 20:21:46 +00:00Commented Mar 15, 2016 at 20:21
16 Answers 16
J, (削除) 31 (削除ここまで) (削除) 30 (削除ここまで) (削除) 14 (削除ここまで) (削除) 12 (削除ここまで) 11 bytes
[:;<@|.`</.
(削除) Ych. Too big. (削除ここまで)
Takes a matrix as input.
Explanation
J has an advantage here. There's a command called oblique (/.) which takes the oblique lines in turn and applies a verb to them. In this case I'm using a gerund to apply two verbs alternately: < (box) and <@|. (reverse and box). Then it's just a matter of unboxing everything using ; (raze).
-
30\$\begingroup\$ J is the only language that makes me feel like I need an advanced degree in English to understand it. \$\endgroup\$Alex A.– Alex A.2016年03月15日 23:35:16 +00:00Commented Mar 15, 2016 at 23:35
-
3\$\begingroup\$ @AlexA. btw, the word "command" should have been "adverb". \$\endgroup\$Adám– Adám2016年03月16日 18:57:28 +00:00Commented Mar 16, 2016 at 18:57
Jelly, (削除) 24 (削除ここまで) (削除) 19 (削除ここまで) (削除) 15 (削除ここまで) (削除) 13 (削除ここまで) 11 bytes
pS€żị"\pỤị5
Takes number of rows, number of columns and a flat list as separate command-line arguments.
How it works
pS€żị"\pỤị5 Main link. Argument: m (rows), n (columns), A (list, flat)
p Compute the Cartesian product [1, ..., m] ×ばつ [1, ..., n]. This yields
the indices of the matrix M, i.e., [[1, 1], [1, 2], ..., [m, n]].
S€ Compute the sums of all index pairs.
p Yield the Cartesian product.
\ Dyadic chain. Arguments: Sums, Cartesian product.
ị" For each index pair in the Cartesian product, retrieve the coordinate
at the index of its sum, i.e., map [i, j] to i if i + j is odd and to
j if i + j is even.
ż Zip the sums with the retrieved indices.
Ụ Sort [1, ..., mn] by the corresponding item in the resulting list.
ị5 Retrieve the corresponding items from A.
-
\$\begingroup\$ Tsk. I'm not sure if I can make mine any shorter now. :-S \$\endgroup\$Gareth– Gareth2016年03月16日 09:30:17 +00:00Commented Mar 16, 2016 at 9:30
-
\$\begingroup\$ That's not to say I won't try though... \$\endgroup\$Gareth– Gareth2016年03月16日 09:33:44 +00:00Commented Mar 16, 2016 at 9:33
-
1\$\begingroup\$ Why hasn't Jelly inherited Oblique yet? May I suggest the APL glyphs
⍁and⍂? Or maybe Scandinavianøandǿ? \$\endgroup\$Adám– Adám2016年03月16日 18:58:29 +00:00Commented Mar 16, 2016 at 18:58
Pyth, (削除) 24 (削除ここまで) (削除) 23 (削除ここまで) (削除) 21 (削除ここまで) (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) (削除) 18 (削除ここまで) 17 bytes
ssm_W=!Td.T+LaYkQ
Alternate 17-byte version: ssuL_G=!T.T+LaYkQ
Q input
+L prepend to each subarray...
aYk (Y += ''). Y is initialized to [], so this prepends [''] to
the first subarray, ['', ''] to the second, etc.
['' 1 2 3 4
'' '' 5 6 7 8
'' '' '' 9 1 2 3]
.T transpose, giving us
['' '' ''
1 '' ''
2 5 ''
3 6 9
4 7 1
8 2
3]
m_W=!Td black magic
s join subarrays together
s join *everything* on empty string (which means ''s used for
padding disappear)
Thanks to @FryAmTheEggman for a byte, @Jakube for 2 bytes, and @isaacg for a byte!
Explanation of "black magic" alluded to above: m_W=!Td essentially reverses every other subarray. It does this by mapping _W=!T over each subarray; W is conditional application, so it _s (reverses) all subarrays where =!T is true. T is a variable preinitialized to ten (truthy), and =!T means (T = !T). So it toggles the value of a variable that starts out truthy and returns the new value, which means that it will alternate between returning falsy, truthy, falsy, truthy... (credit to Jakube for this idea)
MATL, (削除) 28 (削除ここまで) 27 bytes
tZyZ}:w:!+-1y^7MGn/*-X:K#S)
Adapted from my answer here. The general idea is to create a 2D array of the same size as the input, filled with values that increase in the same order as the zig-zag path. Then the linearized (flattened) version of that array is sorted, and the indices of that sorting are kept. Those are the indices that need to be applied to the input in order to produce the zig-zag path.
Input is in the form
[1 2 3; 5 6 4; 9 7 8; 1 2 3]
Explanation
t % input 2D array. Duplicate
ZyZ} % get size as length-2 vector. Split into two numbers: r, c
: % range [1,2,...,c] as a row vector
w:! % swap, range [1;2;...;r] as a column vector
+ % add with broadcast. Gives a 2D array
-1 % push -1
y^ % duplicate previous 2D array. Compute -1 raised to that
7M % push [1;2;...;r] again
Gn/ % divide by input matrix size, that is, r*c
* % multiply
- % subtract
X: % linearize 2D array into column array
K#S % sort and push the indices of the sorting. Gives a column vector
) % index input matrix with that column vector
Matlab, 134 bytes
I just tried my best to shorten my code in Matlab, like telegraphing it.
function V=z(M)
[m,n]=size(M);
a=(1:m)'*ones(1,n);
b=ones(m,1)*(1:n);
A=a+b-1;
B=a-b;
C=(A.^2+(-1).^A.*B+1);
[~,I]=sort(C(:));
V=M(:);
V=V(I)';
Notes:
Mis an×ばつnmatrix.aandbare both matrices same size ofM, each row ofaconsists of numbers equal to its row number, while each column ofbis equal to its column number. Thus,a+bis a matrix whose element equals to sum of its row and column number, i.e.,matrix(p,q)=p+q.- Thus,
A(p,q)=p+q-1; andB(p,q)=p-q. Cis mathematically stated as equation below. Zigzagifiedly increasing matrix with the equation, a zigzagifiedly increasing matrix can be made like shown below.
C = 1 2 6 7 3 5 8 14 4 9 13 18 10 12 19 25
Cindicates the order of elements of M in zigzagified results. Then,[~,I]=sort(C(:));returns the order, i.e.,I, thus,V=V(I)'is the result.
-
\$\begingroup\$ Yes, I just found it, now I update it. \$\endgroup\$Guoyang Qin– Guoyang Qin2016年03月16日 13:50:27 +00:00Commented Mar 16, 2016 at 13:50
-
\$\begingroup\$ @AlexA. Thank you, Alex. For I am new to this and I wanna shorten it as short as possible but make it a snippet. Now I have fixed my code yet. \$\endgroup\$Guoyang Qin– Guoyang Qin2016年03月17日 01:02:47 +00:00Commented Mar 17, 2016 at 1:02
-
\$\begingroup\$ Looks good. Nice first post! :) \$\endgroup\$Alex A.– Alex A.2016年03月17日 01:54:26 +00:00Commented Mar 17, 2016 at 1:54
JavaScript (SpiderMonkey 30+), 99 bytes
x=>[for(y of[...x,...x[0]].keys())for(z of Array(y+1).keys())if(a=x[y%2?z:y-z])if(b=a[y%2?y-z:z])b]
Tested in Firefox 44. Takes input as a 2D array.
Python 2, 84 bytes
lambda N,w,h:[N[i*w+s-i]for s in range(w+h+1)for i in range(h)[::s%2*2-1]if-1<s-i<w]
Porting nimi's answer. Takes a flat array with given width and height. xsot saved a byte.
88 bytes:
lambda M,w,h:[M[i]for i in sorted(range(w*h),key=lambda i:(i/w+i%w,-i*(-1)**(i/w+i%w)))]
Takes a flat array with given width and height. Sorts the corresponding 2D coordinates (i/w,i%w) in zigzag order of increasing sum to get diagonals, tiebroken by either increasing or decreasing row value, based on whether the row plus column is odd or even.
Haskell, (削除) 79 (削除ここまで) (削除) 78 (削除ここまで) 73 bytes
(m#h)w=[m!!(y*w+x-y)|x<-[0..h+w],y<-g!!x$[0..x],y<h,x-y<w]
g=reverse:id:g
The input is a flat list with the number of rows and columns, e.g. ( [1,2,6,3,1,2,5,9,4,7,8,3] # 2) 6 -> [1,2,5,9,6,3,4,7,1,2,8,3].
How it works: walk through the x and y coordinates of the matrix (h rows, w columns) in two nested loops:
| 0 1 2 3 4 5 6 7 8 outer loop Index is y*w+x-y, i.e.
--+------------------ x from 0 to h+w the elements are accessed
0 | 1 2 6 3 1 2 in the following order:
1 | 5 9 4 7 8 3
2 | 1 2 4 6 8 10
3 | 3 5 7 9 11 12
4 |
5 |
6 |
7 | inner loop:
8 | y from 0 to x
i.e. from top/right to down/left, skipping out of bound indices (y and x must satisfy y<h and x-y<w). When x is even, the order of the inner loop is reversed: y goes from x to 0. I do this by picking a modifying function for the y-range [0..x] which is the xth element of [reverse,id,reverse,id,...].
Edit: @xnor re-arranged the loops and saved 5 bytes. Thanks!
-
\$\begingroup\$ I think you can do
g=id:reverse:g. \$\endgroup\$xnor– xnor2016年03月16日 01:05:33 +00:00Commented Mar 16, 2016 at 1:05 -
\$\begingroup\$ The parens on the multication
(y-x)*wcan be cut by transposing the problem:(m#h)w=[m!!(x*w+y-x)|y<-[0..h+w],x<-g!!y$[0..y],x<h,y-x<w] g=reverse:id:g. Translating into Python saves 3 chars over what I had. \$\endgroup\$xnor– xnor2016年03月16日 01:48:36 +00:00Commented Mar 16, 2016 at 1:48
K (ngn/k), (削除) 50 (削除ここまで) 41 bytes
-9 Can use odometer directly
{(,/x)@,/((#c)#(|:;::))@'c:.=+/!#'(x;*x)}
-
\$\begingroup\$ I just worked out that this is the same as @Razetime’s Husk solution but with fewer built-ins available. \$\endgroup\$doug– doug2022年11月10日 03:38:43 +00:00Commented Nov 10, 2022 at 3:38
Python 3, (削除) 131 (削除ここまで) (削除) 118 (削除ここまで) (削除) 115 (削除ここまで) 107 bytes
Based on the same principe as my answer of Deusovi's challenge
I assume we can't have zero in the input matrice
e=enumerate
lambda s:[k for j,i in e(zip(*[([0]*n+i+[0]*len(s))for n,i in e(s)]))for k in i[::j%2*2-1]if k]
Explanation
how it works :
pad with 0 transpose remove 0 reverse line concatenate
with even index
1 2 3 1 2 3 0 0 1 0 0 1 1
4 5 6 --> 0 4 5 6 0 --> 2 4 0 --> 2 4 --> 2 4 --> 1 2 4 7 5 3 6 8 9
7 8 9 0 0 7 8 9 3 5 7 3 5 7 7 5 3
0 6 8 6 8 6 8
0 0 9 9 9
Results
>>> [print([i,f(i)]) for i in [[[1]], [[1, 2], [3, 1]], [[1, 2, 3, 1]], [[1, 2, 3], [5, 6, 4], [9, 7, 8], [1, 2, 3]], [[1, 2, 3, 4], [5, 6, 7, 8], [9, 1, 2, 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]]]]
# [input, output]
[[[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]]
-
\$\begingroup\$ Should
reverse even linebereverse odd linesinstead? \$\endgroup\$nwp– nwp2016年03月16日 10:20:53 +00:00Commented Mar 16, 2016 at 10:20 -
\$\begingroup\$ @nwp index begin at 0 ^^ \$\endgroup\$Erwan– Erwan2016年03月16日 11:58:20 +00:00Commented Mar 16, 2016 at 11:58
-
\$\begingroup\$ Ah, you are talking about the line numbers, not the length of the line. I confused those, sorry. \$\endgroup\$nwp– nwp2016年03月16日 12:00:20 +00:00Commented Mar 16, 2016 at 12:00
-
\$\begingroup\$ @nwp np, btw I changed it to avoid confusion \$\endgroup\$Erwan– Erwan2016年03月16日 12:07:27 +00:00Commented Mar 16, 2016 at 12:07
Julia 0.7, (削除) 114 (削除ここまで) 85 bytes
^ =size
~A=[[i%2<1?reverse(x):x for(i,x)=pairs(-A^2:A^1.|>x->diag(rotl90(A),x))]...;]
Takes the antidiagonals (rotates and takes the main diagonals), reverses every other one, and flattens the list.
-29 bytes thanks to MarcMush:
- replace
A[end:-1:1,1:end]withrotl90(A)to rotate the matrix - rewrite with
pairsinstead ofeachindex - to flatten a vector of vectors
Q, use[Q...;]instead ofvcat[Q...]
Python 2 + NumPy, 122 bytes
I admit it. I worked ahead. Unfortunately, this same method cannot be easily modified to solve the other 2 related challenges...
import numpy
def f(m):w=len(m);print sum([list(m[::-1,:].diagonal(i)[::(i+w+1)%2*-2+1])for i in range(-w,w+len(m[0]))],[])
Takes a numpy array as input. Outputs a list.
Explanation:
def f(m):
w=len(m) # the height of the matrix, (at one point I thought it was the width)
# get the anti-diagonals of the matrix. Reverse them if odd by mapping odd to -1
d=[list(m[::-1,:].diagonal(i)[::(i+w+1)%2*-2+1])for i in range(-w,w+len(m[0]))]
# w+len(m[0]) accounts for the width of the matrix. Works if it's too large.
print sum(d,[]) # join the lists
A lambda is the same length:
import numpy
lambda m:sum([list(m[::-1,:].diagonal(i)[::(i+len(m)+1)%2*-2+1])for i in range(-len(m),len(m)+len(m[0]))],[])
05AB1E, 11 bytes
ΣN2‰ÐOŠO<è‚
Inputs as flattened_list, width, height (even though I ignore the height input).
Try it online or verify all test cases.
Explanation:
The actual sorting algorithm is a direct port of my 05AB1E answer for the Traverse a rectangle's antidiagonals challenge. Even though the actual path over the matrix is different for both challenges, that linked challenge uses \$y,x\$ as coordinates, whereas I use \$x,y\$ here, so the actual sorting implementation is basically the exact same.
Σ # Sort the first (implicit) input-list by:
N # Push the 0-based sort-index
2‰ # Divmod it by the second input-width
Ð # Triplicate this coordinate
O # Sum the top pair together
Š # Tripleswap from pair,pair,sum to sum,pair,pair
O # Sum the top pair together again
< # Decrease it by 1
è # Modular 0-based index this sum-1 into the pair
‚ # Pair it together with the other sum
# (after which the sorted list is output implicitly)
Just like in my linked answer, there are a few equal-bytes alternatives for ÐOŠO<è‚ (not as many as in the linked answer, since we don't have access to the pair as variable y here, since we've just created the pair manually with N2‰):
RDODŠè‚
ÐO<è‚€O
...
JavaScript (Node.js), 89 bytes
a=>a.flatMap((b,i)=>b.map((c,j)=>({c,k:++i-j/(-2)**i}))).sort((p,q)=>p.k-q.k).map(x=>x.c)