Diamondize a Matrix

J, 7 bytes


This is an unnamed a monadic verb which takes a matrix and returns a list of antidiagonals:

   input =. i.3 4
0 1  2  3
4 5  6  7
8 9 10 11

   <@|./. input
│0│4 1│8 5 2│9 6 3│10 7│11│

Test it here.


  • /. is J's built-in to apply a function to each anti-diagonal. Unfortunately, these anti-diagonals are given in the opposite order of what we want here.
  • In <@|., we first apply |. which reverses the anti-diagonal and then < to box it (which is the only way to return a ragged array in J, since normal arrays are always rectangular, so the antidiagonals would be padded with zeroes).

Python, 91 bytes

lambda M:[[r[n-i]for i,r in e(M)if-1<n-i<len(r)][::-1]for n,_ in e(M[1:]+M[0])]

Test it on Ideone.

Python + NumPy, 69 bytes

import numpy
lambda M:map(M[::-1].diagonal,range(1-len(M),len(M[0])))

Expects a 2D NumPy array as input and returns a list of NumPy arrays. Test it on Ideone.

Jelly, 7 bytes


Try it online!


Ṛ         Reverse the matrix vertically.
 ŒD       Get its diagonals. However these start from 
          the main diagonal, not the corners.
    ZL$   Get the width of the input matrix.
   ṙ      Rotate the list of diagonals left by that many 
          places to obtain the correct order.