a = np.array([2,3,1,4])
b = np.array([2,3,7,1])
c = np.zeros((4, 10))
I wanna assign value 1 to some elements in c. a and b define the positions of such elements. a is the starting column indices of value 1 in each row. And b represents how many consecutive 1 there are in the row. The output I am expecting is:
array([[ 0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
I can use a simple for loop as below:
for i in range(c.shape[0]):
for k in range(a[i], a[i]+b[i]):
c[i,k]=1
But it would be slow for large arrays, is there any faster numpy indexing to do this? Thanks.