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
reshape,expend_dimsor the[:,:,None]idiom can be used to add a dimension.np.repeat(ornp.tile) can repeat the values.np.repeat(array_1[:, :, None], Nz, axis=2).