changeset 14368:5736d93b22d0

griddata.m: Accept vectors in any orientation (Bug #33539) * griddata.m: Accept vectors in any orientation (Bug #33539)
author Rik <octave@nomad.inbox5.com>
date Thu, 16 Feb 2012 10:37:07 -0800
parents ba01a38bc5c1
children 932ba2bb9060
files scripts/geometry/griddata.m
diffstat 1 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/geometry/griddata.m	Wed Feb 15 18:41:12 2012 -0500
+++ b/scripts/geometry/griddata.m	Thu Feb 16 10:37:07 2012 -0800
@@ -38,11 +38,8 @@
 ##              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)
+function [rx, ry, rz] = griddata (x, y, z, xi, yi, method = "linear")
 
-  if (nargin == 5)
-    method = "linear";
-  endif
   if (nargin < 5 || nargin > 7)
     print_usage ();
   endif
@@ -51,27 +48,32 @@
     method = tolower (method);
   endif
 
+  ## Meshgrid if x and y are vectors but z is matrix
   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
+    
+  if (isvector (x) && isvector (y) && isvector (z))
+    if (! isequal (length (x), length (y), length (z)))
+      error ("griddata: X, Y, and Z must be vectors of the same length");
     endif
+  elseif (! size_equal (x, y, z))
+    error ("griddata: lengths of X, Y must match the columns and rows of Z");
   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);
+  elseif (isvector (xi) && isvector (yi))
+    ## Otherwise, convert to column vectors
+    xi = xi(:);
+    yi = yi(:);
   endif
 
   if (! size_equal (xi, yi))
     error ("griddata: XI and YI must be vectors or matrices of same size");
   endif
 
-  [nr, nc] = size (xi);
-
   x = x(:);
   y = y(:);
   z = z(:);
@@ -138,6 +140,7 @@
   elseif (nargout == 0)
     mesh (xi, yi, zi);
   endif
+
 endfunction
 
 
@@ -183,3 +186,18 @@
 %! zz2(isnan (zz)) = NaN;
 %! assert (zz, zz2, 100*eps);
 
+%% Test input validation
+%!error griddata ()
+%!error griddata (1)
+%!error griddata (1,2)
+%!error griddata (1,2,3)
+%!error griddata (1,2,3,4)
+%!error griddata (1,2,3,4,5,6,7)
+%!error <vectors of the same length> griddata (1:3, 1:3, 1:4, 1:3, 1:3)
+%!error <vectors of the same length> griddata (1:3, 1:4, 1:3, 1:3, 1:3)
+%!error <vectors of the same length> griddata (1:4, 1:3, 1:3, 1:3, 1:3)
+%!error <the columns and rows of Z> griddata (1:4, 1:3, ones (4,4), 1:3, 1:3)
+%!error <the columns and rows of Z> griddata (1:4, 1:3, ones (3,5), 1:3, 1:3)
+%!error <matrices of same size> griddata (1:3, 1:3, 1:3, 1:4, 1:3)
+%!error <matrices of same size> griddata (1:3, 1:3, 1:3, 1:3, 1:4)
+