1## Copyright (C) 2007, 2008 David Bateman
3## This file is part of Octave.
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.
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.
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/>.
20## @deftypefn {Function File} {@var{yi} =} griddatan (@var{x}, @var{y}, @var{xi}, @var{method}, @var{options})
22## Generate a regular mesh from irregular data using interpolation.
23## The function is defined by @code{@var{y} = f (@var{x})}.
24## The interpolation points are all @var{xi}.
26## The interpolation method can be @code{"nearest"} or @code{"linear"}.
27## If method is omitted it defaults to @code{"linear"}.
28## @seealso{griddata, delaunayn}
31## Author: David Bateman <dbateman@free.fr>
33function yi = griddatan (x, y, xi, method, varargin)
43 method = tolower (method);
49 if (n != ni || size (y, 1) != m || size (y, 2) != 1)
50 error ("griddatan: dimensional mismatch");
54 ## tri = delaunayn(x, varargin{:});
59 if (strcmp (method, "nearest"))
60 ## search index of nearest point
61 idx = dsearchn (x, tri, xi);
63 yi(valid) = y(idx(valid));
65 elseif (strcmp (method, "linear"))
66 ## search for every point the enclosing triangle
67 [tri_list, bary_list] = tsearchn (x, tri, xi);
69 ## only keep the points within triangles.
70 valid = !isnan (tri_list);
71 tri_list = tri_list(!isnan (tri_list));
72 bary_list = bary_list(!isnan (tri_list), :);
73 nr_t = rows (tri_list);
75 ## assign x,y for each point of simplex
76 xt = reshape (x(tri(tri_list,:),:), [nr_t, n+1, n]);
77 yt = y(tri(tri_list,:));
79 ## Use barycentric coordinate of point to calculate yi
80 yi(valid) = sum (y(tri(tri_list,:)) .* bary_list, 2);
83 error ("griddatan: unknown interpolation method");
89%! [xx,yy] = meshgrid(linspace(-1,1,32));
90%! xi = [xx(:), yy(:)];
91%! x = (2 * rand(100,2) - 1);
92%! x = [x;1,1;1,-1;-1,-1;-1,1];
93%! y = sin(2*(sum(x.^2,2)));
94%! zz = griddatan(x,y,xi,'linear');
95%! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'linear');
96%! assert (zz, zz2, 1e-10)
99%! [xx,yy] = meshgrid(linspace(-1,1,32));
100%! xi = [xx(:), yy(:)];
101%! x = (2 * rand(100,2) - 1);
102%! x = [x;1,1;1,-1;-1,-1;-1,1];
103%! y = sin(2*(sum(x.^2,2)));
104%! zz = griddatan(x,y,xi,'nearest');
105%! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'nearest');
106%! assert (zz, zz2, 1e-10)