Dealing with erroneous values close to NaN values
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Niels Klaver
le 9 Avr 2018
Commenté : Niels Klaver
le 10 Avr 2018
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
David Goodmanson
le 10 Avr 2018
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)
Réponse acceptée
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!