Replace numbers in a matrix depending on neighbor numbers

Hi community,
I have a 14x9 matrix that contains as values NaN, 1 or 2. I need to "clean" this matrix regarding cells containing number 1. If cells containing number 1 are neighbor to NaN cells (both up, down, left, right and diagonal), I need to change this number to a NaN. However, more than checking individually, I need to check that if I have a block of numbers 1 connected to a NaN, all these numbers 1 must be replaced to a NaN. If a cell containing a number 1 is surrounded in all 8-directions by numbers 2, this cell must remain with a number 1.
For example, my input is a matrix like this one:
[2 2 2 1 2 1 NaN NaN NaN;
2 2 2 1 2 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 2 1 NaN NaN NaN;
2 1 2 2 2 1 NaN NaN NaN;
2 2 2 2 2 1 NaN NaN NaN];
And the output I´m expecting is:
[2 2 2 NaN 2 NaN NaN NaN NaN;
2 2 2 NaN 2 NaN NaN NaN NaN;
2 2 2 NaN NaN NaN NaN NaN NaN;
2 2 2 NaN NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 NaN NaN NaN NaN NaN;
2 2 2 2 2 NaN NaN NaN NaN;
2 1 2 2 2 NaN NaN NaN NaN;
2 2 2 2 2 NaN NaN NaN NaN];
Thank you very much for your help!!!.

2 Comments

Diego - to be clear, If cells containing number 1 are neighbor to NaN cells (both up, down, left, right and diagonal), I need to change this number to a NaN), does this mean that at least one neighbour is a NaN or do all neighbours have to be NaN?
Diego, Geoff's comment is NOT the 1 Answer you see on the previous page. There is an answer below. Did you scroll down to see it?

Sign in to comment.

 Accepted Answer

Do you have the Image Processing Toolbox? If so, use isnan() and imreconstruct().
This works:
m = ...
[2 2 2 1 2 1 NaN NaN NaN;
2 2 2 1 2 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 1 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 1 1 NaN NaN NaN;
2 2 2 2 2 1 NaN NaN NaN;
2 1 2 2 2 1 NaN NaN NaN;
2 2 2 2 2 1 NaN NaN NaN]
m2 = m;
m2(isnan(m)) = 1
nansOnly = isnan(m)
mask = imreconstruct(nansOnly, m2 == 1)
% Make 1's nans
m2 = m; % Initialize
m2(mask) = nan % Set 1's touching nans to nan

3 Comments

Diego, I haven't heard back from you - is there some problem with my solution? It gives you exactly what you asked for. Did you even try it?
Diego, here is the output you should have gotten when you ran it:
m2 =
2 2 2 NaN 2 NaN NaN NaN NaN
2 2 2 NaN 2 NaN NaN NaN NaN
2 2 2 NaN NaN NaN NaN NaN NaN
2 2 2 NaN NaN NaN NaN NaN NaN
2 2 2 NaN NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 NaN NaN NaN NaN NaN
2 2 2 2 2 NaN NaN NaN NaN
2 1 2 2 2 NaN NaN NaN NaN
2 2 2 2 2 NaN NaN NaN NaN
It looks the same as what you wanted, don't you agree? If you don't think so, explain why not.
Hi Image Analyst,
your function has helped me sooooo much since the moment you answer this question.
I´m facing a situation where I would like to modify the code adding a condition regarding the amount of NaNs I need to have in order to apply the replacement. More precisely, I would like to add as a condition that in the block of NaNs that can be located beside the 'numeric part of the matrix', should have at least one row with 10 NaNs. Is it possible to add a line to the code for adding this condition?. Thank you very much!!!!!!!!!!!!!

Sign in to comment.

More Answers (1)

Hi Image Analyst,
Your solution works perfectly!. At least in my case I have the Image Processing Toolbox, so I was able to use both the isnan() and the imreconstruct(). Sorry for the delay accepting the answer. I tested it on several images and it runs wonderful. Thank you very much for your help!.

Categories

Products

Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!