OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Take value of nearest (L1) non-zero in three dimensional array

  • Thread starter Thread starter Omroth
  • Start date Start date
O

Omroth

Guest
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:

Code:
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0

output:

Code:
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4

input:

Code:
1 0 2 0
0 3 0 1
2 0 4 0

a valid output:

Code:
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.
<p>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.</p>
<p>I can demonstrate the desired result in 2D:</p>
<p>input:</p>
<pre><code>0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
</code></pre>
<p>output:</p>
<pre><code>1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
</code></pre>
<p>input:</p>
<pre><code>1 0 2 0
0 3 0 1
2 0 4 0
</code></pre>
<p>a valid output:</p>
<pre><code>1 2 2 2
3 3 2 1
2 3 4 1
</code></pre>
<p>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.</p>
 
Top