Given boundary value k
, is there a vectorized way to replace each number n
with consecutive descending numbers from n-1
to k
? For example, if k
is 0 the I'd like to replace np.array([3,4,2,2,1,3,1])
with np.array([2,1,0,3,2,1,0,1,0,1,0,0,2,1,0,0])
. Every item of input array is greater than k
.
I have tried combination of np.repeat
and np.cumsum
but it seems evasive solution:
x = np.array([3,4,2,2,1,3,1])
y = np.repeat(x, x)
t = -np.ones(y.shape[0])
t[np.r_[0, np.cumsum(x)[:-1]]] = x-1
np.cumsum(t)
Is there any other way? I expect smth like inverse of np.add.reduceat
that is able to broadcast integers to decreasing sequences instead of minimizing them.