1 ## Copyright (C) 2008 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2 ## Copyright (C) 2010 VZLU Prague
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
18 ## @deftypefn {Function File} {} cartprod (@var{varargin})
20 ## Computes the cartesian product of given column vectors ( row vectors ).
21 ## The vector elements are assumend to be numbers.
23 ## Alternatively the vectors can be specified by as a matrix, by its columns.
25 ## To calculate the cartesian product of vectors,
26 ## P = A x B x C x D ... . Requires A, B, C, D be column vectors.
27 ## The algorithm is iteratively calcualte the products,
28 ## ( ( (A x B ) x C ) x D ) x etc.
32 ## cartprod(1:2,3:4,0:1)
46 function p = cartprod (varargin)
53 [p{1:nargin}] = ndgrid (varargin{:});
54 p = cat (nargin+1, p{:});
55 p = reshape (p, [], nargin);
59 %!assert(cartprod(1:2,0:1),[1 0; 2 0; 1 1; 2 1])