Prev: LSQR
Next: 8*8 image pixel matrix
From: Young Ryu on 5 Aug 2010 02:08 I'd like to replace NaN by using linear interpolation in the below example. Could you recommend fast way as I am dealing very large data actually. A=[1 2 3 NaN 5 NaN 5 4 6]; I tried this: ind=1:length(A); A1=interp1(ind(~isnan(A)), A(~isnan(A)), ind, 'linear'); However, I need faster way. Thank you!
From: us on 5 Aug 2010 03:24 "Young Ryu" <ryuyr77(a)gmail.com> wrote in message <i3dkg4$lus$1(a)fred.mathworks.com>... > I'd like to replace NaN by using linear interpolation in the below example. Could you recommend fast way as I am dealing very large data actually. > > A=[1 2 3 NaN 5 NaN 5 4 6]; > > I tried this: > ind=1:length(A); > A1=interp1(ind(~isnan(A)), A(~isnan(A)), ind, 'linear'); > > However, I need faster way. > > Thank you! one of the solutions ix=~isnan(A); % <- compute the logical array once... a1=interp1(ind(ix),A(ix),ind,'linear'); us
From: TideMan on 5 Aug 2010 05:14 On Aug 5, 6:08 pm, "Young Ryu" <ryuy...(a)gmail.com> wrote: > I'd like to replace NaN by using linear interpolation in the below example. Could you recommend fast way as I am dealing very large data actually. > > A=[1 2 3 NaN 5 NaN 5 4 6]; > > I tried this: > ind=1:length(A); > A1=interp1(ind(~isnan(A)), A(~isnan(A)), ind, 'linear'); > > However, I need faster way. > > Thank you! Here's a very elegant routine that is part of the (free) tidal analysis toolbox t_tide: 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));
From: Greg Heath on 5 Aug 2010 08:42 On Aug 5, 2:08 am, "Young Ryu" <ryuy...(a)gmail.com> wrote: > I'd like to replace NaN by using linear interpolation in the below example. Could you recommend fast way as I am dealing very large data actually. > > A=[1 2 3 NaN 5 NaN 5 4 6]; > > I tried this: > ind=1:length(A); > A1=interp1(ind(~isnan(A)), A(~isnan(A)), ind, 'linear'); As us has indicated, if A is long only compute ~isnan(A) once. However, if the gaps are long, you might consider linear prediction instead of linear interpolation. Hope this helps. Greg
|
Pages: 1 Prev: LSQR Next: 8*8 image pixel matrix |