Convert/Reshape 3D Matrix to a 2D Matrix

Viewed 2720

I have a 3D matrix X which contains vectors as rows into the 3rd dimension. I would like to extract each such vector X(:, x, y) and save it as a 2D matrix such that X(:, 0, 0) is the first row of the 2D matrix, X(:, 0, 1) the second, and so on. The following crude graphic might help illustrate this:

Crude Sketch

I know that I can create my new 2D matrix and then iterate over the original X to add the vectors, but does somebody have some input on how to do this quick and efficiently?


Example: Given

>>> a = np.arange(9*3).reshape(3,3,3)
>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])

I would like to get the following as rows, though the order of the rows does not matter:

array([[ 0,  9, 18],
       [ 1, 10, 19]],
       ...)
1 Answers
Related