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)
Answer from Alex Riley on Stack OverflowUse 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])
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)
How to add elements to 3 dimensional array in python - Stack Overflow
python - Creating a 3d numpy array matrix using append method - Stack Overflow
python - numpy: Append row to a individual 3D array - Stack Overflow
python - Append Value to 3D array numpy - 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]])
I recommend using numpy for multidimensional arrays. It makes it much more convenient, and much faster. This would look like:
import numpy as np
x = np.zeros((10,20,30)) # Make a 10 by 20 by 30 array
x[0,0,0] = value1
Still, if you don't want to use numpy, or need non-rectangular multi-dimensional arrays, you will need to treat it as a list of lists of lists, and initialize each list:
x = []
x.append([])
x[0].append([])
x[0][0].append(value1)
Edit: Or you could use the compact notation shown in ndpu's answer (x = [[[value1]]]).
If you are creating some 3D sparse array, you can save all the data in a dict:
x={}
x[0,0,0] = 11
x[1,0,0] = 21
x[0,1,1] = 111
or:
from collections import defaultdict
x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))
x[0][0][0] = 11
x[1][0][0] = 21
x[0][0][1] = 111
The most efficient way is to initialize the 3d array first and then sequentially write your data into it. Efficiency here is increased by avoiding unnecessary copies. The gains are however minimal - as long as you are not working with very large arrays (in either of the 3 dimensions).
import numpy as np
arr = np.empty(shape=(2, 100, 100))
arr[0,:,:] = array_2d_1
arr[1,:,:] = array_2d_2
By definition you cannot append anything to an array because when the array is declared in memory it has to reserve as much space as it is going to need.
What you can do is to either declare an array with the known geometry and initial values and then rewrite the new values per row keeping a counter of the rows "appended" or you can double the size of the array when you run out of space.
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(:,:,:)
Use the 'a'flag to append to a file.
numpy.savetxt takes an array structure as input, so we need to reshape it.
p,q,r = x.shape
with open("outfile.txt",'ab') as mfile:
header = strftime("%x %X\n")
np.savetxt(mfile, x.reshape(p*q*r), header=header)
I'm a fan of Pickles :)
import cPickle
import time
import numpy as np
arr = np.array(xrange(100)).reshape(10,10)
#write pickle file
with open('out.p', 'wb') as f:
t = time.asctime()
cPickle.dump(t, f, cPickle.HIGHEST_PROTOCOL)
cPickle.dump(arr, f, cPickle.HIGHEST_PROTOCOL)
#read pickle file
with open('out.p', 'rb') as f:
t = cPickle.load(f)
arr = cPickle.load(f)