Matrix to Vector with python/numpy
<p>Numpy <code>ravel</code> works well if I need to create a vector by reading by rows or by columns. However, I would like to transform a matrix to a 1d array, by using a method that is often used in image processing. This is an example with initial matrix <code>A</code> and final result <code>B</code>:</p>

<pre><code>A = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11],
              [12, 13, 14, 15]])

B = np.array([[ 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15])
</code></pre>

<p>Is there an existing function already that could help me with that? If not, can you give me some hints on how to solve this problem? PS. the matrix <code>A</code> is <code>NxN</code>.</p>

<p>I found discussion of zigzag scan for MATLAB, but not much for <code>numpy</code>.  One project appears to use a hardcoded indexing array for 8x8 blocks</p>

<p><a href="https://github.com/lot9s/lfv-compression/blob/master/scripts/our_mpeg/zigzag.py" rel="nofollow">https://github.com/lot9s/lfv-compression/blob/master/scripts/our_mpeg/zigzag.py</a></p>

<pre><code>ZIG = np.array([[0,  1,  5,  6,  14, 15, 27, 28],
               [2,  4,  7,  13, 16, 26, 29, 42],
               [3,  8,  12, 17, 25, 30, 41, 43],
               [9,  11, 18, 24, 31, 40, 44,53],
               [10, 19, 23, 32, 39, 45, 52,54],
               [20, 22, 33, 38, 46, 51, 55,60],
               [21, 34, 37, 47, 50, 56, 59,61],
               [35, 36, 48, 49, 57, 58, 62,63]])
</code></pre>

<p>Apparently it's used jpeg and mpeg compression.</p>

python numpy