Don't concatenate/append/stack arrays if you can help it, especially big ones. It's very wasteful of memory and slow.
Assign A = np.empty((m, n+2, n+2)) and then fill it with A[i] = np.r_[S1, np.c_[S2, Sc[i], S2], S1]. Or do it vectorized and get rid of the for loops:
A = np.zeros((m, n+2, n+2))
A[:,1:-1,1:-1] = Sc
or even do it in one line:
A = np.pad(Sc, ((0,0),(1,1),(1,1)), 'constant', constant_values = 0)
Answer from Daniel F on Stack Overflowpython - Append 2D array to 3D array, extending third dimension - Stack Overflow
Append a "layer" to 3D-Array with Numpy - Stack Overflow
python - appending 3d arrays in numpy - Stack Overflow
python - numpy: Append row to a individual 3D array - Stack Overflow
Don't concatenate/append/stack arrays if you can help it, especially big ones. It's very wasteful of memory and slow.
Assign A = np.empty((m, n+2, n+2)) and then fill it with A[i] = np.r_[S1, np.c_[S2, Sc[i], S2], S1]. Or do it vectorized and get rid of the for loops:
A = np.zeros((m, n+2, n+2))
A[:,1:-1,1:-1] = Sc
or even do it in one line:
A = np.pad(Sc, ((0,0),(1,1),(1,1)), 'constant', constant_values = 0)
You can try this:
A = np.concatenate([A, [Atmp]])
Use dstack:
>>> np.dstack((A, B)).shape
(480, 640, 4)
This handles the cases where the arrays have different numbers of dimensions and stacks the arrays along the third axis.
Otherwise, to use append or concatenate, you'll have to make B three dimensional yourself and specify the axis you want to join them on:
>>> np.append(A, np.atleast_3d(B), axis=2).shape
(480, 640, 4)
using np.stack should work
but the catch is both arrays should be of 2D form.
np.stack([A,B])
Arrays have a fixed size. You can't append to them. If you know the upper bounds of the array, you can preallocate the array with
np.empty((10, 10, 10))
For a 10x10x10 matrix. You can then keep 3 indices x,y,z to track the actual size you have. Eg, add a new element with:
matrix[x,y,z] = newElement
x += 1
Then when you're done, you can extract the submatrix with
finalMatrix = matrix[:x,:y,:z]
You can use np.empty((x,y,z))
You can find a good explanation in this answer: How to create 3 dimensions matrix in numpy , like matlab a(:,:,:)
I have an array of 64 arrays of size 12, and I'd like to append them together to make an array of shape (8,8,12). How would I do this?
I have images with the shape (3,1920,1080) and i want to save them to an array like so (n,3,1920,1080) where n is image order. This will be done continously in a for loop so i only have access to one image at a time
Sounds like what you really need is a python list of 3D numpy arrays. Appending to a numpy array is possible with np.append or np.concat, but it's very expensive because it forces the entire array to be remade. Is there any reason you want a 4D array?
Is this kind of what you're looking for?
>>> a = np.array([[[1, 1, 1],[1, 1, 1]], [[2, 2, 2],[2, 2, 2]], [[3, 3, 3], [3, 3, 3]]])
>>> b = np.array([[[4, 4, 4],[4, 4, 4]], [[5, 5, 5],[5, 5, 5]], [[6, 6, 6], [6, 6, 6]]])
>>> a.shape
(3, 2, 3)
>>> c = np.array([a, b])
>>> c.shape
(2, 3, 2, 3)