-
Notifications
You must be signed in to change notification settings - Fork 1
/
allcomb.m
138 lines (126 loc) · 5.66 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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)
% for Matlab R2011b
% version 4.0 (feb 2014)
% (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
error(nargchk(1,Inf,nargin)) ;
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
% check for empty inputs
if any(cellfun('isempty',varargin(ii))),
warning('ALLCOMB:EmptyInput','Empty inputs result in an empty output.') ;
A = zeros(0,NC) ;
elseif NC > 1
isCellInput = cellfun(@iscell,varargin) ;
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), varargin,'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(varargin{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(varargin{ii}) ;
% concatenate
A = reshape(cat(NC+1,A{:}),[],NC) ;
end
elseif NC==1,
A = varargin{1}(:) ; % nothing to combine
else % NC==0, there was only the 'matlab' flag argument
A = zeros(0,0) ; % nothing
end
%% License (BSD according to http://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb)
% Copyright (c) 2014, Jos (10584)
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.