Mercurial > octave
annotate scripts/plot/util/meshgrid.m @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | 7854d5752dd2 |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 1996-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
2313 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24033
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
2313 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24033
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
2313 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
2313 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24033
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
1713 | 25 |
3407 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## @deftypefn {} {[@var{xx}, @var{yy}] =} meshgrid (@var{x}, @var{y}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## @deftypefnx {} {[@var{xx}, @var{yy}, @var{zz}] =} meshgrid (@var{x}, @var{y}, @var{z}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## @deftypefnx {} {[@var{xx}, @var{yy}] =} meshgrid (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## @deftypefnx {} {[@var{xx}, @var{yy}, @var{zz}] =} meshgrid (@var{x}) |
17490 | 31 ## Given vectors of @var{x} and @var{y} coordinates, return matrices @var{xx} |
32 ## and @var{yy} corresponding to a full 2-D grid. | |
33 ## | |
34 ## The rows of @var{xx} are copies of @var{x}, and the columns of @var{yy} are | |
35 ## copies of @var{y}. If @var{y} is omitted, then it is assumed to be the same | |
36 ## as @var{x}. | |
37 ## | |
38 ## If the optional @var{z} input is given, or @var{zz} is requested, then the | |
24033
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
39 ## output will be a full 3-D grid. If @var{z} is omitted and @var{zz} is |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
40 ## requested, it is assumed to be the same as @var{y}. |
17490 | 41 ## |
42 ## @code{meshgrid} is most frequently used to produce input for a 2-D or 3-D | |
43 ## function that will be plotted. The following example creates a surface | |
44 ## plot of the ``sombrero'' function. | |
45 ## | |
46 ## @example | |
17495
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
17492
diff
changeset
|
47 ## @group |
17490 | 48 ## f = @@(x,y) sin (sqrt (x.^2 + y.^2)) ./ sqrt (x.^2 + y.^2); |
49 ## range = linspace (-8, 8, 41); | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
50 ## [@var{X}, @var{Y}] = meshgrid (range, range); |
17490 | 51 ## Z = f (X, Y); |
52 ## surf (X, Y, Z); | |
17495
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
17492
diff
changeset
|
53 ## @end group |
17490 | 54 ## @end example |
55 ## | |
56 ## Programming Note: @code{meshgrid} is restricted to 2-D or 3-D grid | |
57 ## generation. The @code{ndgrid} function will generate 1-D through N-D | |
58 ## 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
|
59 ## 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
|
60 ## @code{meshgrid} will produce an output grid which is NxM@. @code{ndgrid} |
17514
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17495
diff
changeset
|
61 ## will produce an output which is @nospell{MxN} (transpose) for the same |
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17495
diff
changeset
|
62 ## input. Some core functions expect @code{meshgrid} input and others expect |
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17495
diff
changeset
|
63 ## @code{ndgrid} input. Check the documentation for the function in question |
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17495
diff
changeset
|
64 ## to determine the proper input format. |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
65 ## @seealso{ndgrid, mesh, contour, surf} |
3407 | 66 ## @end deftypefn |
1713 | 67 |
5378 | 68 function [xx, yy, zz] = meshgrid (x, y, z) |
1713 | 69 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
70 if (nargin == 0) |
6046 | 71 print_usage (); |
5717 | 72 endif |
73 | |
74 if (nargin < 2) | |
1713 | 75 y = x; |
76 endif | |
5717 | 77 |
17490 | 78 ## Use repmat to ensure that result values have the same type as the inputs |
6813 | 79 |
24033
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
80 if (nargout < 3 && nargin < 3) |
17490 | 81 if (! (isvector (x) && isvector (y))) |
82 error ("meshgrid: X and Y must be vectors"); | |
1713 | 83 endif |
17490 | 84 xx = repmat (x(:).', length (y), 1); |
85 yy = repmat (y(:), 1, length (x)); | |
1713 | 86 else |
5717 | 87 if (nargin < 3) |
88 z = y; | |
89 endif | |
17490 | 90 if (! (isvector (x) && isvector (y) && isvector (z))) |
91 error ("meshgrid: X, Y, and Z must be vectors"); | |
5717 | 92 endif |
17490 | 93 lenx = length (x); |
94 leny = length (y); | |
95 lenz = length (z); | |
96 xx = repmat (repmat (x(:).', leny, 1), [1, 1, lenz]); | |
97 yy = repmat (repmat (y(:), 1, lenx), [1, 1, lenz]); | |
98 zz = reshape (repmat (z(:).', lenx*leny, 1)(:), leny, lenx, lenz); | |
1713 | 99 endif |
100 | |
101 endfunction | |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
102 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
104 %!test |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
105 %! x = 1:2; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
106 %! y = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
107 %! z = 1:4; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
108 %! [XX, YY, ZZ] = meshgrid (x, y, z); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
109 %! assert (size_equal (XX, YY, ZZ)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
110 %! assert (ndims (XX), 3); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
111 %! assert (size (XX), [3, 2, 4]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
112 %! 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
|
113 %! 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
|
114 |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
115 %!test |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
116 %! x = 1:2; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
117 %! y = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
118 %! [XX, YY] = meshgrid (x, y); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
119 %! assert (size_equal (XX, YY)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
120 %! assert (ndims (XX), 2); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
121 %! assert (size (XX), [3, 2]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
122 %! 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
|
123 %! 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
|
124 |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
125 %!test |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
126 %! x = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
127 %! [XX1, YY1] = meshgrid (x, x); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
128 %! [XX2, YY2] = meshgrid (x); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
129 %! assert (size_equal (XX1, XX2, YY1, YY2)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
130 %! assert (ndims (XX1), 2); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
131 %! assert (size (XX1), [3, 3]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11523
diff
changeset
|
132 %! assert (XX1, XX2); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 %! assert (YY1, YY2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 |
24033
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
135 %!test |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
136 %! x = 1:2; |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
137 %! y = 1:3; |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
138 %! z = 1:4; |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
139 %! [XX, YY] = meshgrid (x, y, z); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
140 %! assert (size_equal (XX, YY)); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
141 %! assert (ndims (XX), 3); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
142 %! assert (size (XX), [3, 2, 4]); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
143 %! assert (XX(1) * YY(1), x(1) * y(1)); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
144 %! assert (XX(end) * YY(end), x(end) * y(end)); |
631076eab47b
Fix dimensions of mesh generated by meshgrid (bug #48523).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23220
diff
changeset
|
145 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
146 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
147 %!error <Invalid call> meshgrid () |
17490 | 148 %!error <X and Y must be vectors> meshgrid (ones (2,2), 1:3) |
149 %!error <X and Y must be vectors> meshgrid (1:3, ones (2,2)) | |
150 %!error <X, Y, and Z must be vectors> [X,Y,Z] = meshgrid (1:3, 1:3, ones (2,2)) |