Mercurial > hg > octave-nkf
annotate scripts/plot/util/meshgrid.m @ 20260:48aa3d6d3427
ru_RU.ts: Add translations from Dmitry Roshchin
author | Andreas Weber <andy.weber.aw@gmail.com> |
---|---|
date | Wed, 08 Apr 2015 21:39:24 +0200 |
parents | 9fc020886ae9 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19790
diff
changeset
|
1 ## Copyright (C) 1996-2015 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1713 | 18 |
3407 | 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 | 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 | 24 ## Given vectors of @var{x} and @var{y} coordinates, return matrices @var{xx} |
25 ## and @var{yy} corresponding to a full 2-D grid. | |
26 ## | |
27 ## The rows of @var{xx} are copies of @var{x}, and the columns of @var{yy} are | |
28 ## copies of @var{y}. If @var{y} is omitted, then it is assumed to be the same | |
29 ## as @var{x}. | |
30 ## | |
31 ## If the optional @var{z} input is given, or @var{zz} is requested, then the | |
32 ## output will be a full 3-D grid. | |
33 ## | |
34 ## @code{meshgrid} is most frequently used to produce input for a 2-D or 3-D | |
35 ## function that will be plotted. The following example creates a surface | |
36 ## plot of the ``sombrero'' function. | |
37 ## | |
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 | 40 ## f = @@(x,y) sin (sqrt (x.^2 + y.^2)) ./ sqrt (x.^2 + y.^2); |
41 ## range = linspace (-8, 8, 41); | |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
42 ## [@var{X}, @var{Y}] = meshgrid (range, range); |
17490 | 43 ## Z = f (X, Y); |
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 | 46 ## @end example |
47 ## | |
48 ## Programming Note: @code{meshgrid} is restricted to 2-D or 3-D grid | |
49 ## generation. The @code{ndgrid} function will generate 1-D through N-D | |
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} |
17514
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17495
diff
changeset
|
53 ## 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
|
54 ## 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
|
55 ## @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
|
56 ## to determine 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 | 58 ## @end deftypefn |
1713 | 59 |
2314 | 60 ## Author: jwe |
61 | |
5378 | 62 function [xx, yy, zz] = meshgrid (x, y, z) |
1713 | 63 |
5717 | 64 if (nargin == 0 || nargin > 3) |
6046 | 65 print_usage (); |
5717 | 66 endif |
67 | |
68 if (nargin < 2) | |
1713 | 69 y = x; |
70 endif | |
5717 | 71 |
17490 | 72 ## Use repmat to ensure that result values have the same type as the inputs |
6813 | 73 |
5717 | 74 if (nargout < 3) |
17490 | 75 if (! (isvector (x) && isvector (y))) |
76 error ("meshgrid: X and Y must be vectors"); | |
1713 | 77 endif |
17490 | 78 xx = repmat (x(:).', length (y), 1); |
79 yy = repmat (y(:), 1, length (x)); | |
1713 | 80 else |
5717 | 81 if (nargin < 3) |
82 z = y; | |
83 endif | |
17490 | 84 if (! (isvector (x) && isvector (y) && isvector (z))) |
85 error ("meshgrid: X, Y, and Z must be vectors"); | |
5717 | 86 endif |
17490 | 87 lenx = length (x); |
88 leny = length (y); | |
89 lenz = length (z); | |
90 xx = repmat (repmat (x(:).', leny, 1), [1, 1, lenz]); | |
91 yy = repmat (repmat (y(:), 1, lenx), [1, 1, lenz]); | |
92 zz = reshape (repmat (z(:).', lenx*leny, 1)(:), leny, lenx, lenz); | |
1713 | 93 endif |
94 | |
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 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
129 ## Test input validation |
17490 | 130 %!error meshgrid () |
131 %!error meshgrid (1,2,3,4) | |
132 %!error <X and Y must be vectors> meshgrid (ones (2,2), 1:3) | |
133 %!error <X and Y must be vectors> meshgrid (1:3, ones (2,2)) | |
134 %!error <X, Y, and Z must be vectors> [X,Y,Z] = meshgrid (1:3, 1:3, ones (2,2)) | |
135 |