Numpy append 2d array

Viewed 43

Let's say I have a bunch of 2d arrays shape (32,32) that I want to be apart of a larger 3d array.

I don't know how many 2d arrays there are so they must be appended.

I've tried stacking but that only works for the first 2 arrays.

What I want is to have a big array of shape (0, 32, 32) that when I append the first 2d array will become (1, 32, 32) then (2, 32, 32) but so far nothing has worked for me.

1 Answers

You can try reshaping the 2d arrays into 3d arrays with one dimension in the axis 0:

big_array = np.zeros((1,32,32))

2d_array = np.ones((32,32))

big_array = np.append(big_array, 2d_array.reshape(1,32,32), axis=0)
big_array.shape
>>> (2,32,32)
Related