Fill NaN cells using surrounding data values
Show older comments
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
Accepted Answer
More Answers (2)
Image Analyst
on 3 Aug 2011
0 votes
It can't be that hard - did you know there is an "isnan()" function, as well as mean() and sum()? Give it a shot yourself first - it's not hard.
James
on 3 Aug 2011
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!