48
\$\begingroup\$

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction:

enter image description here

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 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

\$\endgroup\$
11
  • 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
    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
    Mar 15, 2016 at 19:23
  • 1
    \$\begingroup\$ @Gareth yes you can take a matrix type as input. \$\endgroup\$ 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\$ 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
    Mar 15, 2016 at 20:21

16 Answers 16

31
\$\begingroup\$

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).

\$\endgroup\$
2
  • 29
    \$\begingroup\$ J is the only language that makes me feel like I need an advanced degree in English to understand it. \$\endgroup\$
    – Alex A.
    Mar 15, 2016 at 23:35
  • 3
    \$\begingroup\$ @AlexA. btw, the word "command" should have been "adverb". \$\endgroup\$
    – Adám
    Mar 16, 2016 at 18:57
13
\$\begingroup\$

Jelly, 24 19 15 13 11 bytes

pS€żị"¥pỤị⁵

Takes number of rows, number of columns and a flat list as separate command-line arguments.

Try it online!

How it works

pS€żị"¥pỤị⁵  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.
        ị⁵   Retrieve the corresponding items from A.
\$\endgroup\$
3
  • \$\begingroup\$ Tsk. I'm not sure if I can make mine any shorter now. :-S \$\endgroup\$
    – Gareth
    Mar 16, 2016 at 9:30
  • \$\begingroup\$ That's not to say I won't try though... \$\endgroup\$
    – Gareth
    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
    Mar 16, 2016 at 18:58
11
\$\begingroup\$

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)

Test suite here.

\$\endgroup\$
0
7
\$\begingroup\$

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

Try it online!

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
\$\endgroup\$
6
\$\begingroup\$

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:

  1. M is an m×n matrix.
  2. a and b are both matrices same size of M, each row of a consists of numbers equal to its row number, while each column of b is equal to its column number. Thus, a+b is a matrix whose element equals to sum of its row and column number, i.e., matrix(p,q)=p+q.
  3. Thus, A(p,q)=p+q-1; and B(p,q)=p-q.
  4. C is 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
  1. C indicates 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.
\$\endgroup\$
3
  • \$\begingroup\$ Yes, I just found it, now I update it. \$\endgroup\$ 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\$ Mar 17, 2016 at 1:02
  • \$\begingroup\$ Looks good. Nice first post! :) \$\endgroup\$
    – Alex A.
    Mar 17, 2016 at 1:54
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That if condition can be further shortened. \$\endgroup\$
    – xsot
    Mar 16, 2016 at 10:37
  • \$\begingroup\$ @xsot Nice catch. \$\endgroup\$
    – xnor
    Mar 16, 2016 at 10:43
3
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ I think you can do g=id:reverse:g. \$\endgroup\$
    – xnor
    Mar 16, 2016 at 1:05
  • \$\begingroup\$ The parens on the multication (y-x)*w can 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
    Mar 16, 2016 at 1:48
3
\$\begingroup\$

Husk, 6 bytes

Σz*İ_∂

Try it online!

Same idea as Zgarb's Boustrophedonize solution.

\$\endgroup\$
3
\$\begingroup\$

K (ngn/k), 50 41 bytes

-9 Can use odometer directly

{(,/x)@,/((#c)#(|:;::))@'c:.=+/!#'(x;*x)}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I just worked out that this is the same as @Razetime’s Husk solution but with fewer built-ins available. \$\endgroup\$
    – doug
    Nov 10, 2022 at 3:38
2
\$\begingroup\$

Vyxal, 6 bytes

Þ`⁽ṘẆf

Try it Online!

Þ`     # Antidiagonals
  ⁽ Ẇ  # To every other item
   Ṙ   # Reverse
     f # Flatten
\$\endgroup\$
2
\$\begingroup\$

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))]...;]

Try it online!

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] with rotl90(A) to rotate the matrix
  • rewrite with pairs instead of eachindex
  • to flatten a vector of vectors Q, use [Q...;] instead of vcat[Q...]
\$\endgroup\$
1
  • \$\begingroup\$ 88 bytes. It might look like a new answer but it's yours optimized \$\endgroup\$
    – MarcMush
    Sep 23, 2023 at 14:30
1
\$\begingroup\$

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.

Try it online

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]))],[])
\$\endgroup\$
1
\$\begingroup\$

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]]
\$\endgroup\$
4
  • \$\begingroup\$ Should reverse even line be reverse odd lines instead? \$\endgroup\$
    – nwp
    Mar 16, 2016 at 10:20
  • \$\begingroup\$ @nwp index begin at 0 ^^ \$\endgroup\$
    – Erwan
    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
    Mar 16, 2016 at 12:00
  • \$\begingroup\$ @nwp np, btw I changed it to avoid confusion \$\endgroup\$
    – Erwan
    Mar 16, 2016 at 12:07
0
\$\begingroup\$

05AB1E, 11 bytes

ΣN²‰Ð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
  ²‰         #  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 N²‰):

RDODŠè‚
ÐO<è‚€O
...
\$\endgroup\$
0
\$\begingroup\$

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)

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.