Mercurial > hg > octave-nkf
annotate scripts/plot/util/ndgrid.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 777f26aa8e3e |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17902
diff
changeset
|
1 ## Copyright (C) 2006-2015 Alexander Barth |
5837 | 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. | |
5837 | 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/>. | |
5837 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9040
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x1}, @var{x2}, @dots{}, @var{x}n) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9040
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x}) |
20383
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
22 ## Given n vectors @var{x1}, @dots{}, @var{x}n, @code{ndgrid} returns n |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## arrays of dimension n. |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## The elements of the i-th output argument contains the elements of the |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## vector @var{x}i repeated over all dimensions different from the i-th |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
27 ## dimension. Calling ndgrid with only one input argument @var{x} is |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
28 ## equivalent to calling ndgrid with all n input arguments equal to @var{x}: |
5837 | 29 ## |
14373
d00900b3dc4b
doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
30 ## [@var{y1}, @var{y2}, @dots{}, @var{y}n] = ndgrid (@var{x}, @dots{}, @var{x}) |
17495
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
31 ## |
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
32 ## Programming Note: @code{ndgrid} is very similar to the function |
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
33 ## @code{meshgrid} except that the first two dimensions are transposed in |
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
34 ## comparison to @code{meshgrid}. Some core functions expect @code{meshgrid} |
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
35 ## input and others expect @code{ndgrid} input. Check the documentation for |
db92cd6117a9
ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents:
16778
diff
changeset
|
36 ## the function in question to determine the proper input format. |
5837 | 37 ## @seealso{meshgrid} |
38 ## @end deftypefn | |
39 | |
40 ## Author: Alexander Barth <abarth@marine.usf.edu> | |
41 | |
5838 | 42 function varargout = ndgrid (varargin) |
5837 | 43 |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
44 if (nargin == 0) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
45 print_usage (); |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
46 elseif (nargin == 1) |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 nd = max (nargout, 1); |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 ## If only one input argument is given, repeat it nd-times |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 varargin(1:nd) = varargin(1); |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
50 elseif (nargin >= nargout) |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 nd = max (nargin, 1); |
5837 | 52 else |
53 error ("ndgrid: wrong number of input arguments"); | |
54 endif | |
55 | |
56 ## Determine the size of the output arguments | |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
57 shape = zeros (1, nd); |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 for i = 1:nd |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
59 if (! isvector (varargin{i}) && ! isempty (varargin{i})) |
5837 | 60 error ("ndgrid: arguments must be vectors"); |
61 endif | |
5838 | 62 shape(i) = length (varargin{i}); |
5837 | 63 endfor |
64 | |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 if (nd == 1) |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 ## Special case, single input vector |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 varargout{1} = varargin{1}(:); |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
68 else |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 for i = 1:nd |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 ## size for reshape |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 r = ones (1, nd+1); |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 r(i) = shape(i); |
5837 | 73 |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
74 ## size for repmat |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
75 s = shape; |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
76 s(i) = 1; |
5837 | 77 |
17902
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 varargout{i} = repmat (reshape (varargin{i}, r), s); |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
79 endfor |
9bcf1614cd80
ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
80 endif |
5837 | 81 |
82 endfunction | |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
83 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
85 %!test |
16675
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
86 %! x = 1:3; |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
87 %! assert (isequal (ndgrid (x), x(:))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
88 |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
89 %!test |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
90 %! x = 1:3; |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
91 %! [XX, YY] = ndgrid (x); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
92 %! assert (size_equal (XX, YY)); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
93 %! assert (isequal (XX, repmat(x(:), 1, numel(x)))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
94 %! assert (isequal (YY, repmat(x, numel(x), 1))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
95 |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
96 %!test |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
97 %! x = 1:2; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
98 %! y = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
99 %! z = 1:4; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
100 %! [XX, YY, ZZ] = ndgrid (x, y, z); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
101 %! assert (size_equal (XX, YY, ZZ)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
102 %! assert (ndims (XX), 3); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
103 %! assert (size (XX), [2, 3, 4]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
104 %! 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:
11587
diff
changeset
|
105 %! 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:
11587
diff
changeset
|
106 |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
107 %!test |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
108 %! x = 1:2; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
109 %! y = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
110 %! [XX1, YY1] = meshgrid (x, y); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
111 %! [XX2, YY2] = ndgrid (x, y); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
112 %! assert (size_equal (XX1, YY1)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
113 %! assert (size_equal (XX2, YY2)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
114 %! assert (ndims (XX1), 2); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
115 %! assert (size (XX1), [3, 2]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
116 %! assert (size (XX2), [2, 3]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
117 %! assert (XX2(1) * YY2(1), x(1) * y(1)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
118 %! assert (XX2(end) * YY2(end), x(end) * y(end)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
119 %! assert (XX1, XX2.'); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
120 %! assert (YY1, YY2.'); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
122 %!assert (ndgrid ([]), zeros(0,1)) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
123 %!assert (ndgrid ([], []), zeros(0,0)) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
124 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
125 ## Test input validation |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
126 %!error ndgrid () |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
127 %!error <wrong number of input arguments> [a,b,c] = ndgrid (1:3,1:3) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
128 %!error <arguments must be vectors> ndgrid (ones (2,2)) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
129 |