I am trying to fill the NaN values of grid data with 8 surrounding values but could not understand, what is going wrong here in my matlab code. The data is 752*891*11 %%% (11 years precipitation of 752*891 cells).
for i = 2 : 752 for j = 2 : 891 for k = 1 : 11 if isnan(data(i,j,k)) == 1 data(i,j,k) = nanmean(nanmean(data(i-2:i+2,j-2:j+2,k-2:k+2))); end end endend
Thanks in advance for help
Best Answer
If you want to replace a scalar based on a 3D array, you either need 3 nanmean calls:
with i-1:i+1, instead of i-2:i+2. This would be easier:
But this is a 3x3x3 neighborhood with 27 elements, not 8. I assume you mean:
Because the center point data(i,j,k) is included 3 times, but ignored by nanmean, you have 6 neighbors now, not 8.
So please explain again, what you exactly want.