5837
|
1 ## Copyright (C) 2006, Alexander Barth |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{y1}, @var{y2}, ..., @var{y}n]} = ndgrid (@var{x1}, @var{x2}, ..., @var{x}n) |
|
22 ## @deftypefnx {Function File} {[@var{y1}, @var{y2}, ..., @var{y}n]} = ndgrid (@var{x}) |
|
23 ## Given n vectors @var{x1}, ... @var{x}n, ndgrid returns n arrays of dimension n. |
|
24 ## The elements of the ith output argument contains the elements of the vector |
|
25 ## @var{x}i repeated over all dimensions different from the ith dimension. |
|
26 ## Calling ndgrid with only one input argument @var{x} is equivalent of calling ndgrid with |
|
27 ## all n input arguments equal to @var{x}: |
|
28 ## |
|
29 ## [@var{y1}, @var{y2}, ..., @var{y}n] = ndgrid (@var{x}, ..., @var{x}) |
|
30 ## @seealso{meshgrid} |
|
31 ## @end deftypefn |
|
32 |
|
33 ## Author: Alexander Barth <abarth@marine.usf.edu> |
|
34 |
5838
|
35 function varargout = ndgrid (varargin) |
5837
|
36 |
|
37 if (nargin == 1) |
5838
|
38 n = max ([nargout, 2]); |
5837
|
39 ## If only one input argument is given, repeat it n-times |
|
40 varargin{1:n} = varargin{1}; |
|
41 elseif (nargin >= nargout) |
5838
|
42 n = max ([nargin, 2]); |
5837
|
43 else |
|
44 error ("ndgrid: wrong number of input arguments"); |
|
45 endif |
|
46 |
|
47 ## Determine the size of the output arguments |
|
48 |
5838
|
49 shape = zeros (1, n); |
5837
|
50 |
5838
|
51 for i = 1:n |
|
52 if (! isvector (varargin{i})) |
5837
|
53 error ("ndgrid: arguments must be vectors"); |
|
54 endif |
|
55 |
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 |