I wish to fill all zero valued elements of a three-dimensional numpy array with the value of the “nearest” non-zero valued element (at the point of running the program.) I do not mind which is used when there are multiple with the same distance.
I can demonstrate the desired result in 2D:
input:
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
output:
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
input:
1 0 2 0
0 3 0 1
2 0 4 0
a valid output:
1 2 2 2
3 3 2 1
2 3 4 1
I guess the best I’ve thought of myself is to take each non-zero index in turn and fill neighboring zero valued elements with that value, and repeat until no non-zero elements remain. Which would take ages. This will be running on arrays of size ~ 256,256,256 so being fast would be good.