Hi all, I am dealing with some soil data, that is structured in a 500×400 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 NaNNaN NaN 0.220 NaN NaNNaN NaN 0.219 0.221 NaN
Best Answer