1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
########################################################################
##
## Copyright (C) 2006-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################
## -*- texinfo -*-
## @deftypefn {} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x1}, @var{x2}, @dots{}, @var{x}n)
## @deftypefnx {} {[@var{y1}, @var{y2}, @dots{}, @var{y}n] =} ndgrid (@var{x})
## Given n vectors @var{x1}, @dots{}, @var{x}n, @code{ndgrid} returns n
## arrays of dimension n.
##
## The elements of the i-th output argument contains the elements of the
## vector @var{x}i repeated over all dimensions different from the i-th
## dimension. Calling ndgrid with only one input argument @var{x} is
## equivalent to calling ndgrid with all n input arguments equal to @var{x}:
##
## [@var{y1}, @var{y2}, @dots{}, @var{y}n] = ndgrid (@var{x}, @dots{}, @var{x})
##
## Programming Note: @code{ndgrid} is very similar to the function
## @code{meshgrid} except that the first two dimensions are transposed in
## comparison to @code{meshgrid}. Some core functions expect @code{meshgrid}
## input and others expect @code{ndgrid} input. Check the documentation for
## the function in question to determine the proper input format.
## @seealso{meshgrid}
## @end deftypefn
function varargout = ndgrid (varargin)
if (nargin == 0)
print_usage ();
elseif (nargin == 1)
nd = max (nargout, 1);
## If only one input argument is given, repeat it nd-times
varargin(1:nd) = varargin(1);
elseif (nargin >= nargout)
nd = max (nargin, 1);
else
error ("ndgrid: wrong number of input arguments");
endif
## Determine the size of the output arguments
shape = zeros (1, nd);
for i = 1:nd
if (! isvector (varargin{i}) && ! isempty (varargin{i}))
error ("ndgrid: arguments must be vectors");
endif
shape(i) = length (varargin{i});
endfor
if (nd == 1)
## Special case, single input vector
varargout{1} = varargin{1}(:);
else
for i = 1:nd
## size for reshape
r = ones (1, nd+1);
r(i) = shape(i);
## size for repmat
s = shape;
s(i) = 1;
varargout{i} = repmat (reshape (varargin{i}, r), s);
endfor
endif
endfunction
%!test
%! x = 1:3;
%! assert (isequal (ndgrid (x), x(:)));
%!test
%! x = 1:3;
%! [XX, YY] = ndgrid (x);
%! assert (size_equal (XX, YY));
%! assert (isequal (XX, repmat (x(:), 1, numel (x))));
%! assert (isequal (YY, repmat (x, numel (x), 1)));
%!test
%! x = 1:2;
%! y = 1:3;
%! z = 1:4;
%! [XX, YY, ZZ] = ndgrid (x, y, z);
%! assert (size_equal (XX, YY, ZZ));
%! assert (ndims (XX), 3);
%! assert (size (XX), [2, 3, 4]);
%! assert (XX(1) * YY(1) * ZZ(1), x(1) * y(1) * z(1));
%! assert (XX(end) * YY(end) * ZZ(end), x(end) * y(end) * z(end));
%!test
%! x = 1:2;
%! y = 1:3;
%! [XX1, YY1] = meshgrid (x, y);
%! [XX2, YY2] = ndgrid (x, y);
%! assert (size_equal (XX1, YY1));
%! assert (size_equal (XX2, YY2));
%! assert (ndims (XX1), 2);
%! assert (size (XX1), [3, 2]);
%! assert (size (XX2), [2, 3]);
%! assert (XX2(1) * YY2(1), x(1) * y(1));
%! assert (XX2(end) * YY2(end), x(end) * y(end));
%! assert (XX1, XX2.');
%! assert (YY1, YY2.');
%!assert (ndgrid ([]), zeros (0,1))
%!assert (ndgrid ([], []), zeros(0,0))
## Test input validation
%!error <Invalid call> ndgrid ()
%!error <wrong number of input arguments> [a,b,c] = ndgrid (1:3,1:3)
%!error <arguments must be vectors> ndgrid (ones (2,2))
|