Q: Reshape ndarray broadcasting values to new axis Assume that I have an array with shape (6, 4, 512, 512) and I have another array of shape (6, 512, 512).

*

*How could I reshape the second array so it has the same shape as the
first one?


*Also, would be possible to propagate the values of the second array
across that new axis?
Edit
The function np.resize does exactly what I need.

A: Is this want you want?
x = np.random.rand(6,4,512,512)
y = np.random.rand(6,512,512)
y_ex = np.expand_dims(y, axis=1)

ones_y = np.ones((6,4,512,512))
y_ = ones_y * y_ex

For example:
x = np.random.rand(3,2,4,4)
y = np.random.rand(3,4,4)
y_ex = np.expand_dims(y, axis=1)


ones_y = np.ones((3,2,4,4))
y_ = ones_y * y_ex

gives you y_ex of shape (3,1,4,4) and then y_ of shape (3,2,4,4) with the values of y_ex repeated across axis 1
y
array([[[0.49055614, 0.28459745, 0.87471246, 0.74127825],
        [0.74965895, 0.77622936, 0.98992284, 0.32420505],
        [0.34014753, 0.84957355, 0.47974344, 0.12784663],
        [0.84201589, 0.5556073 , 0.34622819, 0.34372987]],

       [[0.75703384, 0.26535935, 0.13812319, 0.14055896],
        [0.29595331, 0.61979815, 0.14830348, 0.89501206],
        [0.29457856, 0.58359228, 0.38900858, 0.50324793],
        [0.23427909, 0.14967761, 0.79638139, 0.21718771]],

        [[0.54901808, 0.66504512, 0.93174202, 0.22874321],
        [0.43236616, 0.33947959, 0.8224133 , 0.96464956],
        [0.89242413, 0.72640099, 0.07075724, 0.18180732],
        [0.11402021, 0.47821353, 0.86334281, 0.39736966]]])

and
y_
array([[[[0.49055614, 0.28459745, 0.87471246, 0.74127825],
         [0.74965895, 0.77622936, 0.98992284, 0.32420505],
         [0.34014753, 0.84957355, 0.47974344, 0.12784663],
         [0.84201589, 0.5556073 , 0.34622819, 0.34372987]],

        [[0.49055614, 0.28459745, 0.87471246, 0.74127825],
         [0.74965895, 0.77622936, 0.98992284, 0.32420505],
         [0.34014753, 0.84957355, 0.47974344, 0.12784663],
         [0.84201589, 0.5556073 , 0.34622819, 0.34372987]]],


       [[[0.75703384, 0.26535935, 0.13812319, 0.14055896],
         [0.29595331, 0.61979815, 0.14830348, 0.89501206],
         [0.29457856, 0.58359228, 0.38900858, 0.50324793],
         [0.23427909, 0.14967761, 0.79638139, 0.21718771]],

        [[0.75703384, 0.26535935, 0.13812319, 0.14055896],
         [0.29595331, 0.61979815, 0.14830348, 0.89501206],
         [0.29457856, 0.58359228, 0.38900858, 0.50324793],
         [0.23427909, 0.14967761, 0.79638139, 0.21718771]]],


       [[[0.54901808, 0.66504512, 0.93174202, 0.22874321],
         [0.43236616, 0.33947959, 0.8224133 , 0.96464956],
         [0.89242413, 0.72640099, 0.07075724, 0.18180732],
         [0.11402021, 0.47821353, 0.86334281, 0.39736966]],

        [[0.54901808, 0.66504512, 0.93174202, 0.22874321],
         [0.43236616, 0.33947959, 0.8224133 , 0.96464956],
         [0.89242413, 0.72640099, 0.07075724, 0.18180732],
         [0.11402021, 0.47821353, 0.86334281, 0.39736966]]]])

