I have a very basic question. I think this can be done elegantly in a single line with numpy, but I don't know how. Let's say I have a 2D numpy array with shape (Nx,Ny)
, and I want to add a third dimension with shape Nz
. For every fixed index_x,index_y
I want to repeat the value of the array for all indices across the new z axis.
I can easily do this with basic instructions like
# array_1 has shape (Nx,Ny)
array_2 = np.zeros((Nx,Ny,Nz))
for idx_x in range(Nx):
for idx_y in range(Ny):
array_2[idx_x,idx_y,:] = array_1[idx_x,idx_y]
but I was wondering what is the exact numpy function to do this in a single line
source https://stackoverflow.com/questions/76137156/add-a-new-dimension-and-repeat-values-for-a-numpy-array
Comments
Post a Comment