]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/meshgrid.m
update packages
[CreaPhase.git] / octave_packages / m / plot / meshgrid.m
1 ## Copyright (C) 1996-2012 John W. Eaton
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{xx}, @var{yy}, @var{zz}] =} meshgrid (@var{x}, @var{y}, @var{z})
21 ## @deftypefnx {Function File} {[@var{xx}, @var{yy}] =} meshgrid (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {[@var{xx}, @var{yy}] =} meshgrid (@var{x})
23 ## Given vectors of @var{x} and @var{y} and @var{z} coordinates, and
24 ## returning 3 arguments, return three-dimensional arrays corresponding
25 ## to the @var{x}, @var{y}, and @var{z} coordinates of a mesh.  When
26 ## returning only 2 arguments, return matrices corresponding to the
27 ## @var{x} and @var{y} coordinates of a mesh.  The rows of @var{xx} are
28 ## copies of @var{x}, and the columns of @var{yy} are copies of @var{y}.
29 ## If @var{y} is omitted, then it is assumed to be the same as @var{x},
30 ## and @var{z} is assumed the same as @var{y}.
31 ## @seealso{mesh, contour}
32 ## @end deftypefn
33
34 ## Author: jwe
35
36 function [xx, yy, zz] = meshgrid (x, y, z)
37
38   if (nargin == 0 || nargin > 3)
39     print_usage ();
40   endif
41
42   if (nargin < 2)
43     y = x;
44   endif
45
46   ## Use repmat to ensure that the result values have the same type as
47   ## the arguments.
48
49   if (nargout < 3)
50     if (isvector (x) && isvector (y))
51       xx = repmat (x(:).', length (y), 1);
52       yy = repmat (y(:), 1, length (x));
53     else
54       error ("meshgrid: arguments must be vectors");
55     endif
56   else
57     if (nargin < 3)
58       z = y;
59     endif
60     if (isvector (x) && isvector (y) && isvector (z))
61        lenx = length (x);
62        leny = length (y);
63        lenz = length (z);
64        xx = repmat (repmat (x(:).', leny, 1), [1, 1, lenz]);
65        yy = repmat (repmat (y(:), 1, lenx), [1, 1, lenz]);
66        zz = reshape (repmat (z(:).', lenx*leny, 1)(:), leny, lenx, lenz);
67     else
68       error ("meshgrid: arguments must be vectors");
69     endif
70   endif
71
72 endfunction
73
74 %!test
75 %! x = 1:2;
76 %! y = 1:3;
77 %! z = 1:4;
78 %! [XX, YY, ZZ] = meshgrid (x, y, z);
79 %! assert (size_equal (XX, YY, ZZ));
80 %! assert (ndims (XX), 3);
81 %! assert (size (XX), [3, 2, 4]);
82 %! assert (XX(1) * YY(1) * ZZ(1), x(1) * y(1) * z(1));
83 %! assert (XX(end) * YY(end) * ZZ(end), x(end) * y(end) * z(end));
84
85 %!test
86 %! x = 1:2;
87 %! y = 1:3;
88 %! [XX, YY] = meshgrid (x, y);
89 %! assert (size_equal (XX, YY));
90 %! assert (ndims (XX), 2);
91 %! assert (size (XX), [3, 2]);
92 %! assert (XX(1) * YY(1), x(1) * y(1));
93 %! assert (XX(end) * YY(end), x(end) * y(end));
94
95 %!test
96 %! x = 1:3;
97 %! [XX1, YY1] = meshgrid (x, x);
98 %! [XX2, YY2] = meshgrid (x);
99 %! assert (size_equal (XX1, XX2, YY1, YY2));
100 %! assert (ndims (XX1), 2);
101 %! assert (size (XX1), [3, 3]);
102 %! assert (XX1, XX2);
103 %! assert (YY1, YY2);