Mercurial > hg > octave-lyh
annotate scripts/general/interp1q.m @ 17219:87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
* scripts/plot/area.m, scripts/plot/axis.m, scripts/plot/caxis.m,
scripts/plot/comet.m, scripts/plot/comet3.m, scripts/plot/compass.m,
scripts/plot/contour.m, scripts/plot/contour3.m, scripts/plot/contourf.m,
scripts/plot/cylinder.m, scripts/plot/ellipsoid.m, scripts/plot/errorbar.m,
scripts/plot/feather.m, scripts/plot/fill.m, scripts/plot/loglog.m,
scripts/plot/loglogerr.m, scripts/plot/mesh.m, scripts/plot/meshc.m,
scripts/plot/meshz.m, scripts/plot/pcolor.m, scripts/plot/pie.m,
scripts/plot/pie3.m, scripts/plot/plot.m, scripts/plot/plot3.m,
scripts/plot/polar.m, scripts/plot/private/__bar__.m,
scripts/plot/private/__ezplot__.m, scripts/plot/private/__stem__.m,
scripts/plot/quiver.m, scripts/plot/quiver3.m, scripts/plot/rectangle.m,
scripts/plot/ribbon.m, scripts/plot/rose.m, scripts/plot/scatter.m,
scripts/plot/scatter3.m, scripts/plot/semilogx.m, scripts/plot/semilogxerr.m,
scripts/plot/semilogy.m, scripts/plot/semilogyerr.m, scripts/plot/slice.m,
scripts/plot/sphere.m, scripts/plot/stairs.m, scripts/plot/surf.m,
scripts/plot/surfc.m, scripts/plot/surfl.m, scripts/plot/surfnorm.m,
scripts/time/datetick.m: Don't use ifelse in plot fcns to avoid unnecessary fcn
evaluations.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 09 Aug 2013 19:26:55 -0700 |
parents | bb3a1ec7cc4b |
children |
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 | 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 | |
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 |
17087
bb3a1ec7cc4b
doc: Clarify matrix and ND-array args to interpolation functions
Mike Miller <mtmiller@ieee.org>
parents:
14872
diff
changeset
|
24 ## increasing column vector. If @var{y} is a matrix or an N-dimensional |
bb3a1ec7cc4b
doc: Clarify matrix and ND-array args to interpolation functions
Mike Miller <mtmiller@ieee.org>
parents:
14872
diff
changeset
|
25 ## array, the interpolation is performed on each column of @var{y}. If |
bb3a1ec7cc4b
doc: Clarify matrix and ND-array args to interpolation functions
Mike Miller <mtmiller@ieee.org>
parents:
14872
diff
changeset
|
26 ## @var{y} is a vector, it must be a column vector of the same length as |
bb3a1ec7cc4b
doc: Clarify matrix and ND-array args to interpolation functions
Mike Miller <mtmiller@ieee.org>
parents:
14872
diff
changeset
|
27 ## @var{x}. |
7785 | 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7815
diff
changeset
|
34 ## is relatively small. Therefore, the benefit of using @code{interp1q} |
7785 | 35 ## is relatively small. |
36 ## @seealso{interp1} | |
37 ## @end deftypefn | |
38 | |
39 function yi = interp1q (x, y, xi) | |
40 x = x(:); | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
41 nx = rows (x); |
7785 | 42 szy = size (y); |
9940 | 43 y = y(:,:); |
44 [ny, nc] = size (y); | |
7785 | 45 szx = size (xi); |
46 xi = xi (:); | |
9940 | 47 dy = diff (y); |
48 dx = diff (x); | |
7785 | 49 idx = lookup (x, xi, "lr"); |
50 s = (xi - x (idx)) ./ dx (idx); | |
9940 | 51 yi = bsxfun (@times, s, dy(idx,:)) + y(idx,:); |
52 range = xi < x(1) | !(xi <= x(nx)); | |
53 yi(range,:) = NA; | |
7785 | 54 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
|
55 yi = reshape (yi, [max(szx), szy(2:end)]); |
7785 | 56 else |
57 yi = reshape (yi, [szx, szy(2:end)]); | |
58 endif | |
59 endfunction | |
60 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
61 |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7785
diff
changeset
|
62 %!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
|
63 %! xp = [0:2:10].'; yp = sin (2*pi*xp/5); |
7785 | 64 %! 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
|
65 %! 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
|
66 %!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
|
67 %!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
|
68 %!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
|
69 %!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
|
70 %!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
|
71 %!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
|
72 %!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
|
73 |