MATLAB: How do i find all elements of value R in a matrix that are neighboring an element of value K

matlab functionmatrix manipulation

I have a matrix with values of 1, 0, and 2 and i wish to change all the zeros that neighbor a 2 into 2's. Any efficient way of going forward with this problem?
The final problem that need's to be solved is that i need to "color" all the 0's that can be reached from a group of initially set 2's in the matrix. (run the above "function" n^2 times), one can think of the problem as a peculation problem where the 1's are the blocked sites of a rectangular grid.

Best Answer

  • I got this answer on stack overflow, very elegant: