See if something like this is what you are looking for:
input = [nan; nan;-33.20; -37.98; -40.83;...
-52.36; nan; -28.42; nan; nan; nan;...
-24.74; nan; nan; nan; -26.33; nan;...
nan; nan; -23.10; nan; nan; nan; ...
-25.04; nan; nan];
intermediate = interp1(find(~isnan(input)),input(~isnan(input)),1:numel(input));
output = interp1(find(~isnan(intermediate)),intermediate(~isnan(intermediate)),...
1:numel(intermediate),'nearest','extrap');
Essentially, the first interpolation is just linear and will fill in the NaNs that have values on either side. If you look at the output of this step, you will see that the NaNs remain on the edges.
The second interp1 call gets rid of the outer NaNs using the 'extrap' option, but also just fills them in with the nearest good value (which is what the 'nearest' option does)
This does make the assumption that your inputs are equally spaced. If that is not hte case, you would need to replace the first and last inputs into the interp1 function with something that describes the spacing of the data points.
Note: Edited output line to correct typo
0 Comments
Sign in to comment.