annotate scripts/plot/util/ndgrid.m @ 31212:55415fa6a20f

svgconvert: Use Lossless encoding of images when available (bug #52193) * acinclude.m4: Check that QPainter::LosslessImageRendering flag is available. * octave-svgconvert.cc (draw): Use new flag if available. * print.m (doc): Update word of caution about svgconvert and images.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Mon, 29 Aug 2022 16:36:34 +0200
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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) 2006-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
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/>.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
7 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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: 23220
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.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
14 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
19 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
20 ## 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: 6895
diff changeset
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: 23220
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 ########################################################################
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
25
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20173
diff changeset
27 ## @deftypefn {} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x1}, @var{x2}, @dots{}, @var{x}n)
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20173
diff changeset
28 ## @deftypefnx {} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x})
20173
777f26aa8e3e doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## 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: 19833
diff changeset
30 ## arrays of dimension n.
777f26aa8e3e doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
777f26aa8e3e doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## 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: 19833
diff changeset
33 ## 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: 19833
diff changeset
34 ## 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: 19833
diff changeset
35 ## equivalent to calling ndgrid with all n input arguments equal to @var{x}:
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
36 ##
14373
d00900b3dc4b doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
37 ## [@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
38 ##
db92cd6117a9 ndgrid.m: Add explanation of differences with meshgrid to docstring.
Rik <rik@octave.org>
parents: 16778
diff changeset
39 ## 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
40 ## @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
41 ## 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
42 ## 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
43 ## the function in question to determine the proper input format.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
44 ## @seealso{meshgrid}
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
45 ## @end deftypefn
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
46
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
47 function varargout = ndgrid (varargin)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
48
16778
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
49 if (nargin == 0)
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
50 print_usage ();
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
51 elseif (nargin == 1)
17902
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
52 nd = max (nargout, 1);
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## 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
54 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
55 elseif (nargin >= nargout)
17902
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
56 nd = max (nargin, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
57 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
58 error ("ndgrid: wrong number of input arguments");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
59 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
60
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
61 ## 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
62 shape = zeros (1, nd);
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
63 for i = 1:nd
16778
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
64 if (! isvector (varargin{i}) && ! isempty (varargin{i}))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
65 error ("ndgrid: arguments must be vectors");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
66 endif
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
67 shape(i) = length (varargin{i});
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
68 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
69
17902
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
70 if (nd == 1)
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
71 ## 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
72 varargout{1} = varargin{1}(:);
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
73 else
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
74 for i = 1:nd
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 ## size for reshape
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
76 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
77 r(i) = shape(i);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
78
17902
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
79 ## size for repmat
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
80 s = shape;
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 s(i) = 1;
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
82
17902
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
83 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
84 endfor
9bcf1614cd80 ndgrid.m: Don't call reshape with a single dimension input.
Rik <rik@octave.org>
parents: 17744
diff changeset
85 endif
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
86
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
87 endfunction
13263
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
88
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
89
13263
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
90 %!test
16675
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
91 %! x = 1:3;
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
92 %! assert (isequal (ndgrid (x), x(:)));
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
93
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
94 %!test
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
95 %! x = 1:3;
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
96 %! [XX, YY] = ndgrid (x);
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
97 %! assert (size_equal (XX, YY));
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28896
diff changeset
98 %! assert (isequal (XX, repmat (x(:), 1, numel (x))));
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28896
diff changeset
99 %! assert (isequal (YY, repmat (x, numel (x), 1)));
16675
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
100
558e1ce7247b handle single output case of ndgrid
Clemens Buchacher <drizzd@aon.at>
parents: 14373
diff changeset
101 %!test
13263
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
102 %! x = 1:2;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
103 %! y = 1:3;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
104 %! z = 1:4;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
105 %! [XX, YY, ZZ] = ndgrid (x, y, z);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
106 %! assert (size_equal (XX, YY, ZZ));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
107 %! assert (ndims (XX), 3);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
108 %! assert (size (XX), [2, 3, 4]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
109 %! 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
110 %! 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
111
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
112 %!test
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
113 %! x = 1:2;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
114 %! y = 1:3;
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
115 %! [XX1, YY1] = meshgrid (x, y);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
116 %! [XX2, YY2] = ndgrid (x, y);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
117 %! assert (size_equal (XX1, YY1));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
118 %! assert (size_equal (XX2, YY2));
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
119 %! assert (ndims (XX1), 2);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
120 %! assert (size (XX1), [3, 2]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
121 %! assert (size (XX2), [2, 3]);
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
122 %! 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
123 %! 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
124 %! assert (XX1, XX2.');
a156263b5509 Add tests for meshgrid and ndgrid.
Kai Habel <kai.habel@gmx.de>
parents: 11587
diff changeset
125 %! assert (YY1, YY2.');
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
126
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28896
diff changeset
127 %!assert (ndgrid ([]), zeros (0,1))
16778
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
128 %!assert (ndgrid ([], []), zeros(0,0))
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
129
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
130 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27978
diff changeset
131 %!error <Invalid call> ndgrid ()
16778
e205f5ea826a ndgrid.m: Fix Matlab incompatibility with null inputs (bug #38685).
Rik <rik@octave.org>
parents: 16675
diff changeset
132 %!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
133 %!error <arguments must be vectors> ndgrid (ones (2,2))