changeset 10820:c44c786f87ba

interp1.m: When absent set X equal to the inices of Y.
author Ben Abbott <bpabbott@mac.com>
date Mon, 26 Jul 2010 07:41:23 -0400
parents f3c984d45dcb
children 693e22af08ae
files scripts/ChangeLog scripts/general/interp1.m
diffstat 2 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Jul 26 07:34:37 2010 -0400
+++ b/scripts/ChangeLog	Mon Jul 26 07:41:23 2010 -0400
@@ -1,5 +1,6 @@
 2010-07-26  Ben Abbott <bpabbott@mac.com>
 
+	* 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.
 
--- a/scripts/general/interp1.m	Mon Jul 26 07:34:37 2010 -0400
+++ b/scripts/general/interp1.m	Mon Jul 26 07:41:23 2010 -0400
@@ -18,14 +18,16 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- 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)