how to copy numpy array value into higher dimensions

后端 未结 7 2135
一生所求
一生所求 2020-12-06 10:22

I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would d

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 11:10

    You can push all dims forward, introducing a singleton dim/new axis as the last dim to create a 3D array and then repeat three times along that one with np.repeat, like so -

    arr3D = np.repeat(arr[...,None],3,axis=2)
    

    Here's another approach using np.tile -

    arr3D = np.tile(arr[...,None],3)
    

提交回复
热议问题