Dealing with erroneous values close to NaN values

Hi all, I am dealing with some soil data, that is structured in a 500x400 matrix where the region of interest has a value and all other cells are NaN. Hence on the edges it looks something like this (Note: the numbers in the example are not representative for those of the variable displayed in the image, solely an example):
NaN NaN 0.221 0.219 0.222 0.212 0.0058 0.0054
NaN NaN 0.219 0.220 0.218 0.217 0.0054 0.0057
As you might guess, the values close to 0.22 are incorrect. However this is due to a modelling error my supervisor and I are yet unable to fix. However, I wish to replace all values close to a NaN-value(thus the edges of numbered cells) by the nanmean of its neighbours. something like:
[r,c] = size(variable);
for row = 3:(r-3);
for column = 3:(c-3);
if variable(row,column)>0 && (isnan(variable(row-2,column-2))||isnan(variable(row-2,column-1))||isnan(variable(row-2,column))||isnan(variable(row-2,column+1))||isnan(variable(row-2,column+2))
||isnan(variable(row-1,column-2))||isnan(variable(row-1,column-1))||isnan(variable(row-1,column))||isnan(variable(row-1,column+1))||isnan(variable(row-1,column+2))
||isnan(variable(row,column-2))||isnan(variable(row,column-1))||isnan(variable(row,column))||isnan(variable(row,column+1))||isnan(variable(row,column+2))
||isnan(variable(row+1,column-2))||isnan(variable(row+1,column-1))||isnan(variable(row+1,column))||isnan(variable(row+1,column+1))||isnan(variable(row+1,column+2))
||isnan(variable(row+2,column-2))||isnan(variable(row+2,column-1))||isnan(variable(row+2,column))||isnan(variable(row+2,column+1))||isnan(variable(row+2,column+2)));
H = [variable(row-2,column-2),variable(row-2,column-1),variable(row-2,column),variable(row-2,column+1),variable(row-2,column+2);...
variable(row-1,column-2),variable(row-1,column-1),variable(row-1,column),variable(row-1,column+1),variable(row-1,column+2);...
variable(row,column-2),variable(row,column-1),variable(row,column),variable(row,column+1),variable(row,column+2);...
variable(row+1,column-2),variable(row+1,column-1),variable(row+1,column),variable(row+1,column+1),variable(row+1,column+2);...
variable(row+2,column-2),variable(row+2,column-1),variable(row+2,column),variable(row+2,column+1),variable(row+2,column+2)];
variable(row, column) = nanmean(nanmean(H));
else variable(row, column)=variable(row,column); % if no NaN-value close-by, remain original value
end
end
end
However, this is very consuming in terms of code lines and some faulty number strech even further into the grid, substantially increasing the amount of coding if I want to include those cells, located further away from the NaN-values. Therefore I was wondering whether someone has tried something similar or tackled a similar problem with a different approach. Furthermore, the region of interest is irregularly shaped, consequently, data located in (one could say) 'peninsula of data'(see below) will yield a new value on the basis of the faulty numbers.
NaN NaN NaN NaN NaN
NaN NaN 0.220 NaN NaN
NaN NaN 0.219 0.221 NaN

4 commentaires

Is it important to still have values in that region even though they are inaccurately calculated from the nanmean rather than measured values?
If not you could probably use
doc imerode
if you have the image processing toolbox to remove the boundary pixels within a certain width.
Hi Adam,
Thanks for your input on the matter! Though it is not extremely important that we have values in that region, I do wish to keep those cells occupied with somewhat representative values as for different variables the numerical extent and the amount they 'intrude' into the grid differs a bit, this will probably propagate through during calculations as the cells will interact with each other. However, imerode seems like a valid command to get rid of at least the first couple of faulty values and imrode in combination with my own method will seal the deal!
Hi Niels,
the peninsula problem could be challenging but you can at least save some time and some lines on your example code with
if any(any(isnan(variable(row-2:row+2,column-2:column+2))))
% and
H = variable(row-2:row+2,column-2:column+2)
Hi all!
Solving the error in the preceeding modelling and calculations turned out to be easier than expected. Thank you anyways for your thoughts and tips

Connectez-vous pour commenter.

 Réponse acceptée

Niels Klaver
Niels Klaver le 10 Avr 2018

0 votes

Hi all!
Solving the error in the preceeding modelling and calculations turned out to be easier than expected. Thank you anyways for your thoughts and tips!

Plus de réponses (0)

Catégories

En savoir plus sur Agriculture dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by