I'm trying, unsuccessfully, to combine numpy array slicing and advanced indexing. For example I have a numpy array filled with 1/0's
r = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
I find the indexes of non zero elements :
(nz,) = np.nonzero(r)
I would then like to use the array of non zero indexes to operate on my array r. For each index in r I would like to colour a range (in the below 5) of values forward in r. Something like -
r[nz,:nz:nz+5] = 255
which gives result :
array([ 0, 0, 255, 255, 0, 255, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0])
I would however have expected the following :
array([ 0, 0, 255, 255, 255 , 255, 255, 255 , 255, 255 , 255, 255, 255, 255, 255, 0, 0, 0, 0, 0])
given my indexing [nz,:nz:nz+5] = 255. Which I believe to mean from current index nz to nz+5 set value to 255.
My objective is to avoid having to for-loop iterate over the array for efficiency reasons. I'm relatively new to python and numpy so all suggestions are welcome.