# HG changeset patch # User Ben Abbott # Date 1280144483 14400 # Node ID c44c786f87ba964972eb04f0671244ce9489d981 # Parent f3c984d45dcb1cad2a77f38f3da44f07e16ba1a9 interp1.m: When absent set X equal to the inices of Y. diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,5 +1,6 @@ 2010-07-26 Ben Abbott + * general/interp1.m: When absent set X equal to the inices of Y. * general/interpn.m: Convert interpolation vectors of non-equal length to nd-arrays. diff --git a/scripts/general/interp1.m b/scripts/general/interp1.m --- a/scripts/general/interp1.m +++ b/scripts/general/interp1.m @@ -18,14 +18,16 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi}) +## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi}) +## @deftypefnx {Function File} {@var{yi} =} interp1 (@var{y}, @var{xi}) ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method}) ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap}) ## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, 'pp') ## ## One-dimensional interpolation. Interpolate @var{y}, defined at the ## points @var{x}, at the points @var{xi}. The sample points @var{x} -## must be monotonic. If @var{y} is an array, treat the columns +## must be monotonic. If not specified, @var{x} is taken to be the +## indices of @var{y}. If @var{y} is an array, treat the columns ## of @var{y} separately. ## ## Method is one of: @@ -36,7 +38,7 @@ ## @item 'linear' ## Linear interpolation from nearest neighbors ## @item 'pchip' -## Piece-wise cubic Hermite interpolating polynomial +## Piece-wise cubic hermite interpolating polynomial ## @item 'cubic' ## Cubic interpolation from four nearest neighbors ## @item 'spline' @@ -99,7 +101,7 @@ function yi = interp1 (x, y, varargin) - if (nargin < 3 || nargin > 6) + if (nargin < 2 || nargin > 6) print_usage (); endif @@ -132,6 +134,12 @@ endfor endif + if (isempty (xi) && firstnumeric && ! pp) + xi = y; + y = x; + x = 1:numel(y); + endif + ## reshape matrices for convenience x = x(:); nx = rows (x); @@ -601,3 +609,4 @@ %!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap"), [-2,0.5,4,3,1.5]) %!assert (interp1 ([4,4,3,2,0],[0,1,4,2,1],[1.5,4,4.5], "linear"), [0,1,NA]) +%!assert (interp1 (0:4, 2.5), 1.5)