MATLAB: Fill NaN cells using surrounding data values

fillMATLABnan

Hi,
I would like to fill NaN cells in the rows of a matrix with the mean of the immediately surrounding values.
For example I would like to convert
>> A=magic(5);
>> A(:,2:4)=NaN;
>> A(1:2,:)=NaN;
>> A
A =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
4 NaN NaN NaN 22
10 NaN NaN NaN 3
11 NaN NaN NaN 9
>>
To this:
>> A
A =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
4.0000 13.0000 13.0000 13.0000 22.0000
10.0000 6.5000 6.5000 6.5000 3.0000
11.0000 10.0000 10.0000 10.0000 9.0000
>>
Only converting those NaN cells that are surrounded on the row by non-NaN values.
I hope this is clear.
Thanks very much

Best Answer

You may wish to see John's MATLAB File Exchange contribution inpaint_nans