Narusan - 1 month ago 4
Python Question

# NumPy: fill fields surrounding a 1 in an array

Suppose I have a 4x4 matrix that looks like the following:

``````[[0, 0, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]]
``````

I want to write a function that takes all 4 surrounding fields of the one and turns them into a 1 as well.

The above matrix would become:

``````[[0, 0, 1, 0]
[0, 1, 1, 1]
[0, 0, 1, 0]
[0, 0, 0, 0]]
``````

I know that this is possible using if-statements, but I really want to optimize my code.

The matrix only contains 0's and 1's. If the 1 is at the edge of the matrix, the 1's should not wrap around, i.e. if the most left field is a 1, the most right field still stays at 0. Also, I am using Python 3.5

Is there a more mathematical or concise way to do this?

This looks like binary dilation. There's a function available in SciPy that implements this efficiently:

``````>>> from scipy.ndimage import binary_dilation
>>> x
array([[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])

>>> binary_dilation(x).astype(int)
array([[0, 0, 1, 0],
[0, 1, 1, 1],
[0, 0, 1, 0],
[0, 0, 0, 0]])
``````

1s at the edges are handled as you've specified they should be (i.e. no wrapping).

See the documentation for further options and arguments.