Mercurial > hg > octave-lyh
annotate scripts/plot/ndgrid.m @ 16950:b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
* scripts/plot/fplot.m: Add ability to specify n,tol,fmt in any order and
simultaneously. Return data rather than plotting it if asked. Use
additional test on progress of algorithm to decide whether to quit. Add
%!demo and %!tests.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 11 Jul 2013 09:25:54 -0700 |
parents | e205f5ea826a |
children | db92cd6117a9 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13263
diff
changeset
|
1 ## Copyright (C) 2006-2012 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}) |
6895 | 22 ## Given n vectors @var{x1}, @dots{} @var{x}n, @code{ndgrid} returns |
14373
d00900b3dc4b
doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
23 ## n arrays of dimension n. The elements of the i-th output argument |
6895 | 24 ## contains the elements of the vector @var{x}i repeated over all |
8494 | 25 ## dimensions different from the i-th dimension. Calling ndgrid with |
6895 | 26 ## only one input argument @var{x} is equivalent of calling ndgrid with |
5837 | 27 ## all n input arguments equal to @var{x}: |
28 ## | |
14373
d00900b3dc4b
doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
29 ## [@var{y1}, @var{y2}, @dots{}, @var{y}n] = ndgrid (@var{x}, @dots{}, @var{x}) |
5837 | 30 ## @seealso{meshgrid} |
31 ## @end deftypefn | |
32 | |
33 ## Author: Alexander Barth <abarth@marine.usf.edu> | |
34 | |
5838 | 35 function varargout = ndgrid (varargin) |
5837 | 36 |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
37 if (nargin == 0) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
38 print_usage (); |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
39 elseif (nargin == 1) |
16675
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
40 n = max ([nargout, 1]); |
5837 | 41 ## If only one input argument is given, repeat it n-times |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
42 varargin(1:n) = varargin(1); |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
43 elseif (nargin >= nargout) |
16675
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
44 n = max ([nargin, 1]); |
5837 | 45 else |
46 error ("ndgrid: wrong number of input arguments"); | |
47 endif | |
48 | |
49 ## Determine the size of the output arguments | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
50 |
5838 | 51 shape = zeros (1, n); |
52 for i = 1:n | |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
53 if (! isvector (varargin{i}) && ! isempty (varargin{i})) |
5837 | 54 error ("ndgrid: arguments must be vectors"); |
55 endif | |
5838 | 56 shape(i) = length (varargin{i}); |
5837 | 57 endfor |
58 | |
5838 | 59 for i = 1:n |
5837 | 60 ## size for reshape |
5838 | 61 r = ones (1, n); |
5837 | 62 r(i) = shape(i); |
63 | |
64 ## size for repmat | |
65 s = shape; | |
66 s(i) = 1; | |
67 | |
5838 | 68 varargout{i} = repmat (reshape (varargin{i}, r), s); |
5837 | 69 endfor |
70 | |
71 endfunction | |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
72 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
74 %!test |
16675
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
75 %! x = 1:3; |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
76 %! assert (isequal (ndgrid (x), x(:))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
77 |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
78 %!test |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
79 %! x = 1:3; |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
80 %! [XX, YY] = ndgrid (x); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
81 %! assert (size_equal (XX, YY)); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
82 %! assert (isequal (XX, repmat(x(:), 1, numel(x)))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
83 %! assert (isequal (YY, repmat(x, numel(x), 1))); |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
84 |
558e1ce7247b
handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents:
14373
diff
changeset
|
85 %!test |
13263
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
86 %! x = 1:2; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
87 %! y = 1:3; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
88 %! z = 1:4; |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
89 %! [XX, YY, ZZ] = ndgrid (x, y, z); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
90 %! assert (size_equal (XX, YY, ZZ)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
91 %! assert (ndims (XX), 3); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
92 %! assert (size (XX), [2, 3, 4]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
93 %! 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
|
94 %! 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
|
95 |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
96 %!test |
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 %! [XX1, YY1] = meshgrid (x, y); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
100 %! [XX2, YY2] = ndgrid (x, y); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
101 %! assert (size_equal (XX1, YY1)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
102 %! assert (size_equal (XX2, YY2)); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
103 %! assert (ndims (XX1), 2); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
104 %! assert (size (XX1), [3, 2]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
105 %! assert (size (XX2), [2, 3]); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
106 %! 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
|
107 %! 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
|
108 %! assert (XX1, XX2.'); |
a156263b5509
Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents:
11587
diff
changeset
|
109 %! assert (YY1, YY2.'); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 |
16778
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
111 %!assert (ndgrid ([]), zeros(0,1)) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
112 %!assert (ndgrid ([], []), zeros(0,0)) |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
113 |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
114 %% Test input validation |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
115 %!error ndgrid () |
e205f5ea826a
ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents:
16675
diff
changeset
|
116 %!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
|
117 %!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
|
118 |