0a1 > ## Copyright (C) 2014 Nir Krakauer 39c40 < ## @item @qcode{"nearest"} --- > ## @item @code{"nearest"} 42c43,49 < ## @item @qcode{"linear"} (default) --- > ## @item @code{"previous"} > ## Return the previous neighbor. > ## > ## @item @code{"next"} > ## Return the next neighbor. > ## > ## @item @code{"linear"} (default) 45c52 < ## @item @qcode{"pchip"} --- > ## @item @code{"pchip"} 49,50c56,57 < ## @item @qcode{"cubic"} < ## Cubic interpolation (same as @qcode{"pchip"}). --- > ## @item @code{"cubic"} > ## Cubic interpolation (same as @code{"pchip"}). 52c59 < ## @item @qcode{"spline"} --- > ## @item @code{"spline"} 60c67 < ## and is never slower. The default method is @qcode{"linear"}. --- > ## and is never slower. The default method is @code{"linear"}. 62c69 < ## If @var{extrap} is the string @qcode{"extrap"}, then extrapolate values --- > ## If @var{extrap} is the string @code{"extrap"}, then extrapolate values 67c74 < ## If the string argument @qcode{"pp"} is specified, then @var{xi} should not --- > ## If the string argument @code{"pp"} is specified, then @var{xi} should not 71,72c78,79 < ## @var{y}, @var{method}, @qcode{"pp"}), @var{xi}) == interp1 (@var{x}, @var{y}, < ## @var{xi}, @var{method}, @qcode{"extrap"})}. --- > ## @var{y}, @var{method}, @code{"pp"}), @var{xi}) == interp1 (@var{x}, @var{y}, > ## @var{xi}, @var{method}, @code{"extrap"})}. 80c87 < ## the options, @qcode{"left"} or @qcode{"right"}, to select a left-continuous --- > ## the options @code{"left"} or @code{"right"} to select a left-continuous 82,83c89,90 < ## Discontinuous interpolation is only allowed for @qcode{"nearest"} and < ## @qcode{"linear"} methods; in all other cases, the @var{x}-values must be --- > ## Discontinuous interpolation is only allowed for @code{"nearest"} and > ## @code{"linear"} methods; in all other cases, the @var{x}-values must be 190a198,201 > if strcmp (method, "previous") || strcmp (method, "*previous") || strcmp (method, "next") || strcmp (method, "*next") > rightcontinuous = NaN; #needed for these methods to work > endif > 204a216,226 > #because of the way mkpp works, it's easiest to implement "next" by running "previous" with vectors flipped > if strcmp (method, "next") > x = flipud (x); > y = flipud (y); > method = "previous"; > elseif strcmp (method, "*next") > x = flipud (x); > y = flipud (y); > method = "*previous"; > endif > 248a271,292 > case "previous" > pp = mkpp ([x(1:nx); 2*x(nx)-x(nx-1)], > shiftdim (y, 1), szy(2:end)); > pp.orient = "first"; > > if (ispp) > yi = pp; > else > yi = ppval (pp, reshape (xi, szx)); > endif > > case "*previous" > pp = mkpp (x(1)+[0:nx]*dx, > shiftdim (y, 1), szy(2:end)); > pp.orient = "first"; > > if (ispp) > yi = pp; > else > yi = ppval (pp, reshape (xi, szx)); > endif > 249a294 > 484a530,599 > %!test style = "previous"; > ## BLOCK > %!assert (interp1 (xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]) > %!assert (interp1 (xp,yp,xp,style), yp, 100*eps) > %!assert (interp1 (xp,yp,xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp,style), yp, 100*eps) > %!assert (isempty (interp1 (xp',yp',[],style))) > %!assert (isempty (interp1 (xp,yp,[],style))) > %!assert (interp1 (xp,[yp',yp'],xi(:),style),... > %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) > # %!assert (interp1 (xp,yp,xi,style),... # this test is expected to fail, so commented out -- previous and next options are not symmetric w.r.t flipping xp,yp > # %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) > %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), > %! interp1 (xp,yp,xi,style,"extrap"),10*eps) > %!error interp1 (1,1,1, style) > %!assert (interp1 (xp,[yp',yp'],xi,style), > %! interp1 (xp,[yp',yp'],xi,["*",style]),100*eps) > %!test style = ["*",style]; > %!assert (interp1 (xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]) > %!assert (interp1 (xp,yp,xp,style), yp, 100*eps) > %!assert (interp1 (xp,yp,xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp,style), yp, 100*eps) > %!assert (isempty (interp1 (xp',yp',[],style))) > %!assert (isempty (interp1 (xp,yp,[],style))) > %!assert (interp1 (xp,[yp',yp'],xi(:),style),... > %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) > # %!assert (interp1 (xp,yp,xi,style),... > # %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) > %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), > %! interp1 (xp,yp,xi,style,"extrap"),10*eps) > %!error interp1 (1,1,1, style) > ## ENDBLOCK > > %!test style = "next"; > ## BLOCK > %!assert (interp1 (xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]) > %!assert (interp1 (xp,yp,xp,style), yp, 100*eps) > %!assert (interp1 (xp,yp,xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp,style), yp, 100*eps) > %!assert (isempty (interp1 (xp',yp',[],style))) > %!assert (isempty (interp1 (xp,yp,[],style))) > %!assert (interp1 (xp,[yp',yp'],xi(:),style),... > %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) > # %!assert (interp1 (xp,yp,xi,style),... > # %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) > %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), > %! interp1 (xp,yp,xi,style,"extrap"),10*eps) > %!error interp1 (1,1,1, style) > %!assert (interp1 (xp,[yp',yp'],xi,style), > %! interp1 (xp,[yp',yp'],xi,["*",style]),100*eps) > %!test style = ["*",style]; > %!assert (interp1 (xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]) > %!assert (interp1 (xp,yp,xp,style), yp, 100*eps) > %!assert (interp1 (xp,yp,xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp',style), yp', 100*eps) > %!assert (interp1 (xp',yp',xp,style), yp, 100*eps) > %!assert (isempty (interp1 (xp',yp',[],style))) > %!assert (isempty (interp1 (xp,yp,[],style))) > %!assert (interp1 (xp,[yp',yp'],xi(:),style),... > %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) > # %!assert (interp1 (xp,yp,xi,style),... > # %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) > %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), > %! interp1 (xp,yp,xi,style,"extrap"),10*eps) > %!error interp1 (1,1,1, style) > ## ENDBLOCK > 628c743 < ## test extrapolation (linear) --- > ## test extrapolation 629a745,747 > %!assert (interp1 ([1:5],[3:2:11],[0,6],"nearest","extrap"), [3, 11], eps) > %!assert (interp1 ([1:5],[3:2:11],[0,6],"previous","extrap"), [3, 11], eps) > %!assert (interp1 ([1:5],[3:2:11],[0,6],"next","extrap"), [3, 11], eps) 633a752,753 > %!assert (interp1 (1:2,1:2,1.6,"previous"), 1) > %!assert (interp1 (1:2,1:2,1.4,"next"), 2) 639a760,761 > %!assert (interp1 (1:2:4,1:2:4,2.2,"*previous"), 1) > %!assert (interp1 (1:2:4,1:2:4,1.4,"*next"), 3) 659a782,783 > %!error interp1 (1,1,1, "previous") > %!error interp1 (1,1,1, "*previous") 660a785 > %!error interp1 ([1 1],[1 2],1, "next")