comparison scripts/deprecated/interp1q.m @ 18102:e124ae274013 gui-release

maint: Backout ca72f1b73216 on gui_release branch.
author Rik <rik@octave.org>
date Thu, 05 Dec 2013 11:30:28 -0800
parents
children 4197fc428c7d
comparison
equal deleted inserted replaced
18101:282820308650 18102:e124ae274013
1 ## Copyright (C) 2008-2013 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{yi} =} interp1q (@var{x}, @var{y}, @var{xi})
21 ## One-dimensional linear interpolation without error checking.
22 ## Interpolates @var{y}, defined at the points @var{x}, at the points
23 ## @var{xi}. The sample points @var{x} must be a strictly monotonically
24 ## increasing column vector. If @var{y} is a matrix or an N-dimensional
25 ## array, the interpolation is performed on each column of @var{y}. If
26 ## @var{y} is a vector, it must be a column vector of the same length as
27 ## @var{x}.
28 ##
29 ## Values of @var{xi} beyond the endpoints of the interpolation result
30 ## in NA being returned.
31 ##
32 ## Note that the error checking is only a significant portion of the
33 ## execution time of this @code{interp1} if the size of the input arguments
34 ## is relatively small. Therefore, the benefit of using @code{interp1q}
35 ## is relatively small.
36 ## @seealso{interp1}
37 ## @end deftypefn
38
39 function yi = interp1q (x, y, xi)
40
41 persistent warned = false;
42 if (! warned)
43 warned = true;
44 warning ("Octave:deprecated-function",
45 "interp1q is obsolete and will be removed from a future version of Octave; use interp1 instead");
46 endif
47
48 x = x(:);
49 nx = rows (x);
50 szy = size (y);
51 y = y(:,:);
52 [ny, nc] = size (y);
53 szx = size (xi);
54 xi = xi (:);
55 dy = diff (y);
56 dx = diff (x);
57 idx = lookup (x, xi, "lr");
58 s = (xi - x (idx)) ./ dx (idx);
59 yi = bsxfun (@times, s, dy(idx,:)) + y(idx,:);
60 range = xi < x(1) | !(xi <= x(nx));
61 yi(range,:) = NA;
62 if (length (szx) == 2 && any (szx == 1))
63 yi = reshape (yi, [max(szx), szy(2:end)]);
64 else
65 yi = reshape (yi, [szx, szy(2:end)]);
66 endif
67 endfunction
68
69
70 %!shared xp, yp, xi, yi
71 %! xp = [0:2:10].'; yp = sin (2*pi*xp/5);
72 %! xi = [-1; 0; 2.2; 4; 6.6; 10; 11];
73 %! yi = interp1 (xp,yp,xi);
74 %!assert (interp1q (xp,yp, [min(xp)-1; max(xp)+1]), [NA; NA]);
75 %!assert (interp1q (xp,yp,xp), yp, 100*eps);
76 %!assert (isempty (interp1q (xp,yp,[])));
77 %!assert (interp1q (xp,yp,xi), yi);
78 %!assert (interp1q (xp,[yp,yp],xi), [yi, yi]);
79 %!assert (interp1q (xp,yp,[xi,xi]), [yi, yi]);
80 %!assert (interp1q (xp,[yp,yp],[xi,xi]), cat (3, [yi, yi], [yi, yi]));
81