-
Notifications
You must be signed in to change notification settings - Fork 14
/
allcomb.m
114 lines (104 loc) · 4.35 KB
/
allcomb.m
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
function A = allcomb(varargin)
% ALLCOMB - All combinations
% B = ALLCOMB(A1,A2,A3,...,AN) returns all combinations of the elements
% in the arrays A1, A2, ..., and AN. B is P-by-N matrix is which P is the product
% of the number of elements of the N inputs. This functionality is also
% known as the Cartesian Product. The arguments can be numerical and/or
% characters, or they can be cell arrays.
%
% Examples:
% allcomb([1 3 5],[-3 8],[0 1]) % numerical input:
% % -> [ 1 -3 0
% % 1 -3 1
% % 1 8 0
% % ...
% % 5 -3 1
% % 5 8 1 ] ; % a 12-by-3 array
%
% allcomb('abc','XY') % character arrays
% % -> [ aX ; aY ; bX ; bY ; cX ; cY] % a 6-by-2 character array
%
% allcomb('xy',[65 66]) % a combination
% % -> ['xA' ; 'xB' ; 'yA' ; 'yB'] % a 4-by-2 character array
%
% allcomb({'hello','Bye'},{'Joe', 10:12},{99999 []}) % all cell arrays
% % -> { 'hello' 'Joe' [99999]
% % 'hello' 'Joe' []
% % 'hello' [1x3 double] [99999]
% % 'hello' [1x3 double] []
% % 'Bye' 'Joe' [99999]
% % 'Bye' 'Joe' []
% % 'Bye' [1x3 double] [99999]
% % 'Bye' [1x3 double] [] } ; % a 8-by-3 cell array
%
% ALLCOMB(..., 'matlab') causes the first column to change fastest which
% is consistent with matlab indexing. Example:
% allcomb(1:2,3:4,5:6,'matlab')
% % -> [ 1 3 5 ; 1 4 5 ; 1 3 6 ; ... ; 2 4 6 ]
%
% If one of the arguments is empty, ALLCOMB returns a 0-by-N empty array.
%
% See also NCHOOSEK, PERMS, NDGRID
% and NCHOOSE, COMBN, KTHCOMBN (Matlab Central FEX)
% Tested in Matlab R2015a
% version 4.1 (feb 2016)
% (c) Jos van der Geest
% email: [email protected]
% History
% 1.1 (feb 2006), removed minor bug when entering empty cell arrays;
% added option to let the first input run fastest (suggestion by JD)
% 1.2 (jan 2010), using ii as an index on the left-hand for the multiple
% output by NDGRID. Thanks to Jan Simon, for showing this little trick
% 2.0 (dec 2010). Bruno Luong convinced me that an empty input should
% return an empty output.
% 2.1 (feb 2011). A cell as input argument caused the check on the last
% argument (specifying the order) to crash.
% 2.2 (jan 2012). removed a superfluous line of code (ischar(..))
% 3.0 (may 2012) removed check for doubles so character arrays are accepted
% 4.0 (feb 2014) added support for cell arrays
% 4.1 (feb 2016) fixed error for cell array input with last argument being
% 'matlab'. Thanks to Richard for pointing this out.
narginchk(1,Inf) ;
NC = nargin ;
% check if we should flip the order
if ischar(varargin{end}) && (strcmpi(varargin{end},'matlab') || strcmpi(varargin{end},'john')),
% based on a suggestion by JD on the FEX
NC = NC-1 ;
ii = 1:NC ; % now first argument will change fastest
else
% default: enter arguments backwards, so last one (AN) is changing fastest
ii = NC:-1:1 ;
end
args = varargin(1:NC) ;
% check for empty inputs
if any(cellfun('isempty',args)),
warning('ALLCOMB:EmptyInput','One of more empty inputs result in an empty output.') ;
A = zeros(0,NC) ;
elseif NC > 1
isCellInput = cellfun(@iscell,args) ;
if any(isCellInput)
if ~all(isCellInput)
error('ALLCOMB:InvalidCellInput', ...
'For cell input, all arguments should be cell arrays.') ;
end
% for cell input, we use to indices to get all combinations
ix = cellfun(@(c) 1:numel(c), args,'un',0) ;
% flip using ii if last column is changing fastest
[ix{ii}] = ndgrid(ix{ii}) ;
A = cell(numel(ix{1}),NC) ; % pre-allocate the output
for k=1:NC,
% combine
A(:,k) = reshape(args{k}(ix{k}),[],1) ;
end
else
% non-cell input, assuming all numerical values or strings
% flip using ii if last column is changing fastest
[A{ii}] = ndgrid(args{ii}) ;
% concatenate
A = reshape(cat(NC+1,A{:}),[],NC) ;
end
elseif NC==1,
A = args{1}(:) ; % nothing to combine
else % NC==0, there was only the 'matlab' flag argument
A = zeros(0,0) ; % nothing
end