annotate scripts/plot/meshgrid.m @ 17495:db92cd6117a9

ndgrid.m: Add explanation of differences with meshgrid to docstring. * scripts/plot/meshgrid.m: Add more explanation of differences with ndgrid to docstring. * scripts/plot/ndgrid.m: Add explanation of differences with meshgrid to docstring.
author Rik <rik@octave.org>
date Wed, 25 Sep 2013 16:45:46 -0700
parents ff5ff67946cb
children 5b916efea542
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13263
diff changeset
1 ## Copyright (C) 1996-2012 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6813
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6813
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6813
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6813
diff changeset
17 ## <http://www.gnu.org/licenses/>.
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
18
3407
5e0a0b1cba43 [project @ 2000-01-06 03:13:55 by jwe]
jwe
parents: 2847
diff changeset
19 ## -*- texinfo -*-
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
20 ## @deftypefn {Function File} {[@var{xx}, @var{yy}] =} meshgrid (@var{x}, @var{y})
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
21 ## @deftypefnx {Function File} {[@var{xx}, @var{yy}, @var{zz}] =} meshgrid (@var{x}, @var{y}, @var{z})
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents: 3408
diff changeset
22 ## @deftypefnx {Function File} {[@var{xx}, @var{yy}] =} meshgrid (@var{x})
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
23 ## @deftypefnx {Function File} {[@var{xx}, @var{yy}, @var{zz}] =} meshgrid (@var{x})
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
24 ## Given vectors of @var{x} and @var{y} coordinates, return matrices @var{xx}
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
25 ## and @var{yy} corresponding to a full 2-D grid.
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
26 ##
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
27 ## The rows of @var{xx} are copies of @var{x}, and the columns of @var{yy} are
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
28 ## copies of @var{y}. If @var{y} is omitted, then it is assumed to be the same
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
29 ## as @var{x}.
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
30 ##
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
31 ## If the optional @var{z} input is given, or @var{zz} is requested, then the
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
32 ## output will be a full 3-D grid.
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
33 ##
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
34 ## @code{meshgrid} is most frequently used to produce input for a 2-D or 3-D
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
35 ## function that will be plotted. The following example creates a surface
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
36 ## plot of the ``sombrero'' function.
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
37 ##
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
38 ## @example
17495
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
39 ## @group
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
40 ## f = @@(x,y) sin (sqrt (x.^2 + y.^2)) ./ sqrt (x.^2 + y.^2);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
41 ## range = linspace (-8, 8, 41);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
42 ## [@var{X}, @var{Y}] = meshgrid (range, range);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
43 ## Z = f (X, Y);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
44 ## surf (X, Y, Z);
17495
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
45 ## @end group
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
46 ## @end example
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
47 ##
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
48 ## Programming Note: @code{meshgrid} is restricted to 2-D or 3-D grid
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
49 ## generation. The @code{ndgrid} function will generate 1-D through N-D
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
50 ## grids. However, the functions are not completely equivalent. If @var{x}
17492
ff5ff67946cb meshgrid.m: Close @code{} macro in docstring.
Rik <rik@octave.org>
parents: 17490
diff changeset
51 ## is a vector of length M and @var{y} is a vector of length N, then
17495
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
52 ## @code{meshgrid} will produce an output grid which is NxM@. @code{ndgrid}
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
53 ## will produce an output which is MxN (transpose) for the same input. Some
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
54 ## core functions expect @code{meshgrid} input and others expect @code{ndgrid}
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
55 ## input. Check the documentation for the function in question to determine
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 17492
diff changeset
56 ## the proper input format.
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14363
diff changeset
57 ## @seealso{ndgrid, mesh, contour, surf}
3407
5e0a0b1cba43 [project @ 2000-01-06 03:13:55 by jwe]
jwe
parents: 2847
diff changeset
58 ## @end deftypefn
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
59
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
60 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
61
5378
b2a5596a3f7b [project @ 2005-06-02 16:01:25 by jwe]
jwe
parents: 5307
diff changeset
62 function [xx, yy, zz] = meshgrid (x, y, z)
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
63
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
64 if (nargin == 0 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5717
diff changeset
65 print_usage ();
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
66 endif
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
67
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
68 if (nargin < 2)
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
69 y = x;
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
70 endif
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
71
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
72 ## Use repmat to ensure that result values have the same type as the inputs
6813
75d99621f850 [project @ 2007-08-14 17:14:18 by jwe]
jwe
parents: 6046
diff changeset
73
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
74 if (nargout < 3)
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
75 if (! (isvector (x) && isvector (y)))
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
76 error ("meshgrid: X and Y must be vectors");
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
77 endif
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
78 xx = repmat (x(:).', length (y), 1);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
79 yy = repmat (y(:), 1, length (x));
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
80 else
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
81 if (nargin < 3)
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
82 z = y;
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
83 endif
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
84 if (! (isvector (x) && isvector (y) && isvector (z)))
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
85 error ("meshgrid: X, Y, and Z must be vectors");
5717
34cda7d94c08 [project @ 2006-03-27 22:26:18 by jwe]
jwe
parents: 5642
diff changeset
86 endif
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
87 lenx = length (x);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
88 leny = length (y);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
89 lenz = length (z);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
90 xx = repmat (repmat (x(:).', leny, 1), [1, 1, lenz]);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
91 yy = repmat (repmat (y(:), 1, lenx), [1, 1, lenz]);
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
92 zz = reshape (repmat (z(:).', lenx*leny, 1)(:), leny, lenx, lenz);
1713
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
93 endif
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
94
d9e42d0bab09 [project @ 1996-01-08 02:22:55 by jwe]
jwe
parents:
diff changeset
95 endfunction
13263
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
96
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
97
13263
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
98 %!test
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
99 %! x = 1:2;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
100 %! y = 1:3;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
101 %! z = 1:4;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
102 %! [XX, YY, ZZ] = meshgrid (x, y, z);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
103 %! assert (size_equal (XX, YY, ZZ));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
104 %! assert (ndims (XX), 3);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
105 %! assert (size (XX), [3, 2, 4]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
106 %! assert (XX(1) * YY(1) * ZZ(1), x(1) * y(1) * z(1));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
107 %! assert (XX(end) * YY(end) * ZZ(end), x(end) * y(end) * z(end));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
108
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
109 %!test
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
110 %! x = 1:2;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
111 %! y = 1:3;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
112 %! [XX, YY] = meshgrid (x, y);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
113 %! assert (size_equal (XX, YY));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
114 %! assert (ndims (XX), 2);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
115 %! assert (size (XX), [3, 2]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
116 %! assert (XX(1) * YY(1), x(1) * y(1));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
117 %! assert (XX(end) * YY(end), x(end) * y(end));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
118
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
119 %!test
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
120 %! x = 1:3;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
121 %! [XX1, YY1] = meshgrid (x, x);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
122 %! [XX2, YY2] = meshgrid (x);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
123 %! assert (size_equal (XX1, XX2, YY1, YY2));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
124 %! assert (ndims (XX1), 2);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
125 %! assert (size (XX1), [3, 3]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11523
diff changeset
126 %! assert (XX1, XX2);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
127 %! assert (YY1, YY2);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
128
17490
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
129 %% Test input validation
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
130 %!error meshgrid ()
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
131 %!error meshgrid (1,2,3,4)
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
132 %!error <X and Y must be vectors> meshgrid (ones (2,2), 1:3)
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
133 %!error <X and Y must be vectors> meshgrid (1:3, ones (2,2))
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
134 %!error <X, Y, and Z must be vectors> [X,Y,Z] = meshgrid (1:3, 1:3, ones (2,2))
cff399332a7f meshgrid.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
135