]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/cartprod.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / cartprod.m
1 ## Copyright (C) 2008 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2 ## Copyright (C) 2010 VZLU Prague
3 ##
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
7 ## version.
8 ##
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
12 ## details.
13 ##
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/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} cartprod (@var{varargin})
19 ##
20 ## Computes the cartesian product of given column vectors ( row vectors ).
21 ## The vector elements are assumend to be numbers.
22 ##
23 ## Alternatively the vectors can be specified by as a matrix, by its columns.
24 ##
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.
29 ##
30 ## @example
31 ## @group
32 ##   cartprod(1:2,3:4,0:1)
33 ##   ans =   1   3   0
34 ##           2   3   0
35 ##           1   4   0
36 ##           2   4   0
37 ##           1   3   1
38 ##           2   3   1
39 ##           1   4   1
40 ##           2   4   1
41 ## @end group
42 ## @end example
43 ## @end deftypefn
44 ## @seealso{kron}
45
46 function p = cartprod (varargin)
47    if (nargin < 1)
48      print_usage ();
49    elseif (nargin == 1)
50      p = varargin{1};
51    endif
52
53    [p{1:nargin}] = ndgrid (varargin{:});
54    p = cat (nargin+1, p{:});
55    p = reshape (p, [], nargin);
56
57 endfunction
58
59 %!assert(cartprod(1:2,0:1),[1 0; 2 0; 1 1; 2 1])