rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
createCombinations.m
Go to the documentation of this file.
1 function comb = createCombinations(ranges, varargin)
2  % Creates the cartesian product of the vectors passed as a
3  % matrix containing elements of each vector per row.
4  %
5  % Inputs:
6  % ranges: Can either be a cell array of vectors or a vector.
7  % varargin: If the first argument is a vector, an arbitrary
8  % number of additional vectors can be passed to build the
9  % cartesian product from.
10  %
11  % Return values:
12  % comb: A matrix containing the combinations, each row
13  % corresponds to an input vector's range.
14  %
15  % this file was taken from KerMor
16  % @author Daniel Wirtz @date 11.10.2010
17 
18  if ~isa(ranges,'cell')
19  if isempty(varargin)
20  comb = ranges;
21  return;
22  end
23  r = cell(1,length(varargin)+1);
24  r{1} = ranges;
25  [r{2:end}] = varargin{:};
26  ranges = r;
27  end
28 
29  n = length(ranges);
30  % Create nd-grids
31  [matrices{1:n}] = ndgrid(ranges{:});
32  % Convert to np x params matrix
33  comb = zeros(n,numel(matrices{1}));
34  for idx=1:n
35  % Check if any range is empty - return empty then
36  if isempty(matrices{idx})
37  comb = [];
38  return;
39  end
40 
41  comb(idx,:) = matrices{idx}(:);
42  end
43 end
function comb = createCombinations(ranges, varargin)
Creates the cartesian product of the vectors passed as a matrix containing elements of each vector pe...