Concatenate 3D arrays - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Concatenate 3D arrays (/thread-33248.html) |
Concatenate 3D arrays - paul18fr - Apr-09-2021 Hi I still have difficulties to work with 3D arrays; in the following example, I'm trying to add/concatenate a 2D array to a 3D one. My trials fail so far and some googling didn't helped me. What's the correct use? Thanks for any help import numpy as np A = np.zeros( (2, 3, 5) ) print("A = {}\n".format(A)) ## 1rst term => depth ## 2 other terms => (i,j) ## basic 2D array B = np.ones( (3, 5) ) print("B = {}\n".format(B)) ## 2D array reshaped into 3D one prior to be concatenated Bprime = B.reshape(1,3,5) print("Bprime = {}\n".format(Bprime)) ## C is created and suppoed to be A expanded by B C = np.concatenate( (A, Bprime), axis = 2 ) # C = np.dstack( (A, Bprime)) # C = np.block( [A, Bprime] ) print("C = {}\n".format(C)) RE: Concatenate 3D arrays - paul18fr - Apr-09-2021 Ok I fixed it and I figured out my mistake: axis=0 becomes the depth one for a 3D array and not rows axis as in a 2D array A = np.zeros( (2, 3, 5) ) print("A = {}\n".format(A)) ## 1rst term => depth ## 2 other terms => (i,j) ## basic 2D array B = np.ones( (3, 5) ) print("B = {}\n".format(B)) ## 2D array reshaped into 3D one prior to be concatenated Bprime = B.reshape(1,3,5) print("Bprime = {}\n".format(Bprime)) ## C is created and suppoed to be A expanded by B # C = np.concatenate( (A, Bprime) ) C = np.concatenate( (A, Bprime), axis = 0) print("C = {}\n".format(C)) |