annotate scripts/general/interp1q.m @ 14872:c2dbdeaa25df

maint: use rows() and columns() to clarify m-files. * gradient.m, interp1q.m, rat.m, tsearchn.m, image.m, imwrite.m, area.m, contourc.m, hist.m, isocolors.m, isonormals.m, meshz.m, print.m, __bar__.m, __go_draw_axes__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m, __print_parse_opts__.m, __quiver__.m, rose.m, shrinkfaces.m, stairs.m, surfnorm.m, tetramesh.m, text.m, deconv.m, spline.m, intersect.m, setdiff.m, setxor.m, union.m, periodogram.m, pcg.m, perms.m: Replace size (x,1) with rows (x) and size(x,2) with columns(x).
author Rik <octave@nomad.inbox5.com>
date Tue, 17 Jul 2012 13:34:19 -0700
parents 5d3a684236b0
children bb3a1ec7cc4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1 ## Copyright (C) 2008-2012 David Bateman
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{yi} =} interp1q (@var{x}, @var{y}, @var{xi})
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
21 ## One-dimensional linear interpolation without error checking.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
22 ## Interpolates @var{y}, defined at the points @var{x}, at the points
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7815
diff changeset
23 ## @var{xi}. The sample points @var{x} must be a strictly monotonically
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7815
diff changeset
24 ## increasing column vector. If @var{y} is an array, treat the columns
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7815
diff changeset
25 ## of @var{y} separately. If @var{y} is a vector, it must be a column
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
26 ## vector of the same length as @var{x}.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
27 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 ## Values of @var{xi} beyond the endpoints of the interpolation result
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 ## in NA being returned.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 ##
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 ## Note that the error checking is only a significant portion of the
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 ## execution time of this @code{interp1} if the size of the input arguments
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7815
diff changeset
33 ## is relatively small. Therefore, the benefit of using @code{interp1q}
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 ## is relatively small.
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35 ## @seealso{interp1}
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 ## @end deftypefn
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 function yi = interp1q (x, y, xi)
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 x = x(:);
14872
c2dbdeaa25df maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents: 14868
diff changeset
40 nx = rows (x);
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 szy = size (y);
9940
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
42 y = y(:,:);
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
43 [ny, nc] = size (y);
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 szx = size (xi);
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 xi = xi (:);
9940
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
46 dy = diff (y);
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
47 dx = diff (x);
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 idx = lookup (x, xi, "lr");
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
49 s = (xi - x (idx)) ./ dx (idx);
9940
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
50 yi = bsxfun (@times, s, dy(idx,:)) + y(idx,:);
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
51 range = xi < x(1) | !(xi <= x(nx));
1707df259536 optimize interp1q
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
52 yi(range,:) = NA;
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 if (length (szx) == 2 && any (szx == 1))
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7785
diff changeset
54 yi = reshape (yi, [max(szx), szy(2:end)]);
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 else
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 yi = reshape (yi, [szx, szy(2:end)]);
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 endif
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 endfunction
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
59
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
60
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7785
diff changeset
61 %!shared xp, yp, xi, yi
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
62 %! xp = [0:2:10].'; yp = sin (2*pi*xp/5);
7785
9a656cd34f34 Add the interp1q function
David Bateman <dbateman@free.fr>
parents:
diff changeset
63 %! xi = [-1; 0; 2.2; 4; 6.6; 10; 11];
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7785
diff changeset
64 %! yi = interp1 (xp,yp,xi);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
65 %!assert (interp1q (xp,yp, [min(xp)-1; max(xp)+1]), [NA; NA]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
66 %!assert (interp1q (xp,yp,xp), yp, 100*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
67 %!assert (isempty (interp1q (xp,yp,[])));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
68 %!assert (interp1q (xp,yp,xi), yi);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
69 %!assert (interp1q (xp,[yp,yp],xi), [yi, yi]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
70 %!assert (interp1q (xp,yp,[xi,xi]), [yi, yi]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
71 %!assert (interp1q (xp,[yp,yp],[xi,xi]), cat (3, [yi, yi], [yi, yi]));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
72