Assign a sequence at irregular intervals in 1D array - Python / NumPy

前端 未结 2 517
执念已碎
执念已碎 2021-01-23 08:08

I have a sequence of numbers that I would like to insert into a larger array at irregular intervals:

dates = np.zeros(15)
pattern = np.arange(3) + 1
starts = [2,         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 08:43

    Construct a 2D selector array to select the indices of dates you want to modify with numpy.add.outer, then perform a broadcasted assignment of pattern into the selected indices:

    dates[numpy.add.outer(starts, numpy.arange(len(pattern)))] = pattern
    

提交回复
热议问题