Efficient product of 1D array and 3D array along one dimension - NumPy

后端 未结 2 1269
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 05:32

I have two numpy arrays:

  • A 1D array called t of shape (70L,) with element called let s say ti
  • A 3D array called I with shape (70L, 1024L, 1024L), with ea
2条回答
  •  Happy的楠姐
    2021-01-21 06:18

    Divakar gives the best (most efficient) answers. For completeness' sake, one other way of doing it is by using Numpy's broadcasting capabilities:

    (t[:,np.newaxis,np.newaxis]*I).sum(axis=0)
    

    By adding two axes to t, broadcasting becomes possible and one can use regular Numpy operations, which for some might be more readable.

提交回复
热议问题