Re: interpolate missing data



On May 7, 9:44 am, "Hydroman S" <amirgsa...@xxxxxxxxx> wrote:
% the example

a = magic(4); a(2,2) = NaN ; a(3,3)=NaN
b=inpaint_nans(a)
[M,N] = size(a);
c = a;
for i = 1:M
  c(:,i) = inpaint_nans(a(:,i));
end
c

if I paste above example in matlab 7.0.0.19920 (R14), this
is the output I get:

a =

    16     2     3    13
     5   NaN    10     8
     9     7   NaN    12
     4    14    15     1

b =

           16            2            3           13
            5            6           10            8
            9            7           11           12
            4           14           15            1

Warning: Matrix is singular to working precision.> In inpaint_nans at 170

Warning: Matrix is singular to working precision.

In inpaint_nans at 170

c =

           16            2            3           13
            5          3.6           10            8
            9            7         13.4           12
            4           14           15            1

and if I paste it in matlab 6.1.0.450 (R12.1), this is
what I get:

a =

    16     2     3    13
     5   NaN    10     8
     9     7   NaN    12
     4    14    15     1

??? Error: File: C:\MATLAB6p1\work\inpaint_nans.m Line:
123 Column: 16
Expected a variable, function, or constant, found "|".

Here is an alternative routine that is much simpler than
inpaint_nans. It works on vectors and simply does linear
interpolation across gaps. It comes from Rich Pawlowicz's t_tide
package:
function y=fixgaps(x);
% FIXGAPS Linearly interpolates gaps in a time series
% YOUT=FIXGAPS(YIN) linearly interpolates over NaN
% in the input time series (may be complex), but ignores
% trailing and leading NaN.
%

% R. Pawlowicz 6/Nov/99

y=x;

bd=isnan(x);
gd=find(~bd);

bd([1:(min(gd)-1) (max(gd)+1):end])=0;


y(bd)=interp1(gd,x(gd),find(bd));
.