# HG changeset patch # User Ben Abbott # Date 1283779157 14400 # Node ID aa40bdbfa478b22133c4495ad26075492691ef14 # Parent a7655cdba47083fc16c28c1da004ac07f77ccfb4 griddata.m: Allow x, y inputs to be vectors, and z a matrix. diff -r a7655cdba470 -r aa40bdbfa478 scripts/ChangeLog --- a/scripts/ChangeLog Fri Sep 03 13:18:59 2010 +0200 +++ b/scripts/ChangeLog Mon Sep 06 09:19:17 2010 -0400 @@ -1,3 +1,7 @@ +2010-09-06 Petr Mikulik + + * geometry/griddata.m: Allow x, y to be vectors, and z a matrix. + 2010-07-07 David Bateman * geometry/delaunay.m: Allow the delaunay function to treat diff -r a7655cdba470 -r aa40bdbfa478 scripts/geometry/griddata.m --- a/scripts/geometry/griddata.m Fri Sep 03 13:18:59 2010 +0200 +++ b/scripts/geometry/griddata.m Mon Sep 06 09:19:17 2010 -0400 @@ -19,9 +19,12 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {@var{zi} =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) ## @deftypefnx {Function File} {[@var{xi}, @var{yi}, @var{zi}] =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) -## +## ## Generate a regular mesh from irregular data using interpolation. ## The function is defined by @code{@var{z} = f (@var{x}, @var{y})}. +## Inputs @code{@var{x}, @var{y}, @var{z}} are vectors of the same length +## or @code{@var{x}, @var{y}} are vectors and @code{@var{z}} is matrix. +## ## The interpolation points are all @code{(@var{xi}, @var{yi})}. If ## @var{xi}, @var{yi} are vectors then they are made into a 2D mesh. ## @@ -32,25 +35,32 @@ ## Author: Kai Habel ## Adapted-by: Alexander Barth -## xi and yi are not "meshgridded" if both are vectors +## xi and yi are not "meshgridded" if both are vectors ## of the same size (for compatibility) function [rx, ry, rz] = griddata (x, y, z, xi, yi, method) - + if (nargin == 5) method = "linear"; endif - if (nargin < 5 || nargin > 7) + if (nargin < 5 || nargin > 7) print_usage (); endif if (ischar (method)) method = tolower (method); endif - if (! all (size (x) == size (y) & size (x) == size (z))) - error ("griddata: x, y, and z must be vectors of same length"); + + if (isvector (x) && isvector (y) && all ([numel(y), numel(x)] == size (z))) + [x, y] = meshgrid (x, y); + elseif (! all (size (x) == size (y) & size (x) == size (z))) + if (isvector (z)) + error ("griddata: x, y, and z, be vectors of same length."); + else + error ("griddata: lengths of x, y must match the columns and rows of z."); + endif endif - + ## Meshgrid xi and yi if they are a row and column vector. if (rows (xi) == 1 && columns (yi) == 1) [xi, yi] = meshgrid (xi, yi); @@ -61,7 +71,7 @@ endif [nr, nc] = size (xi); - + x = x(:); y = y(:); z = z(:); @@ -69,7 +79,7 @@ ## Triangulate data. tri = delaunay (x, y); zi = NaN (size (xi)); - + if (strcmp (method, "cubic")) error ("griddata: cubic interpolation not yet implemented"); @@ -107,14 +117,14 @@ N = cross ([x2-x1, y2-y1, z2-z1], [x3-x1, y3-y1, z3-z1]); ## Normalize. N = diag (norm (N, "rows")) \ N; - + ## Calculate D of plane equation ## Ax+By+Cz+D = 0; D = -(N(:,1) .* x1 + N(:,2) .* y1 + N(:,3) .* z1); - + ## Calculate zi by solving plane equation for xi, yi. zi(valid) = -(N(:,1).*xi(:)(valid) + N(:,2).*yi(:)(valid) + D) ./ N(:,3); - + else error ("griddata: unknown interpolation method"); endif