Say I have a numpy array
import numpy as np
arr = np.zeros(20)
arr[::4] = func = np.arange(5) # Excuse the setup hack
print(arr)
i.e.
[0. 0. 0. 0. 1. 0. 0. 0. 2. 0. 0. 0. 3. 0. 0. 0. 4. 0. 0. 0.] ,
How can I right-pad the zeroes in the array without a for-loop to give the following?
arr_expected = np.array([0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4])
I should add that the stride length is irregular/arbitrary - NOT every 4 as in the example.
Here is a second example:
import numpy as np
arr = np.zeros(20)
arr[2]=4
arr[9]=3
arr[18]=101
print(arr)
gives:
[ 0. 0. 4. 0. 0. 0. 0. 0. 0. 3. 0. 0. 0. 0. 0. 0. 0. 0. 101. 0.] ,
with the expected output being
arr_expected = np.array([0,0,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,101,101])
Note that this is not a cumulative sum (although, in this case, func
is a cumulative sum but only at certain indices in an original array). I have thought about impulses and transfer functions, uneven square waves, interpolation, sliding masks.....
PS The zeros could be any other pad value - np.nan
etc. or even an arbitrary array. The point is to fill in the gaps with the values at predefined indices.
Huge thanks as ever