Python: How to increase the dimensions of a matrix without expanding it?

Viewed 2036

I am quite new in Python, and am currently working on a code in which I want to store previous iterations of an 3-dimensional matrix, a version of which is created in each single step of a for-loop. The way I want to go about this is by concatenating a new array of dimension 3+1=4, which stores the previous values. Now this is possible with concatenation, and I got it to work like this:

import numpy as np

matrix = np.ones((1,lay,row,col), dtype=np.float32)

for n in range(100):
    if n == 0:
        # initialize the storage matrix
        matrix_stored = matrix
    else:
        # append further matrices in first dimension
        matrix_stored = np.concatenate((matrix_stored,matrix),axis = 0)

so here's my question: the code above requires that the matrix already is in four-dimensional structure [1 x m x n x o]. For my purposes, however, I would prefer to keep the variable matrix three-dimensional [m x n x o], and only transform it into a four-dimensional form when feeding it into the variable matrix_stored.

Is there a way to facilitate such a conversion?

2 Answers
Related