-
Notifications
You must be signed in to change notification settings - Fork 13
/
combn.m
104 lines (75 loc) · 2.03 KB
/
combn.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
function M = combn(V,N)
% error(nargchk(2,2,nargin)) ;
%
% if isempty(V) || N == 0,
% M = [] ;
% IND = [] ;
% elseif fix(N) ~= N || N < 1 || numel(N) ~= 1 ;
% error('combn:negativeN','Second argument should be a positive integer') ;
% elseif N==1,
% % return column vectors
% M = V(:) ;
% IND = (1:numel(V)).' ;
% else
% % speed depends on the number of output arguments
% if nargout<2,
% M = local_allcomb(V,N) ;
% else
% % indices requested
% IND = local_allcomb(1:numel(V),N) ;
% M = V(IND) ;
% end
% end
%
% function Y = local_allcomb(X,N)
% % See ALLCOMB, available on the File Exchange
% if N>1
% % create a list of all possible combinations of N elements
% [Y{N:-1:1}] = ndgrid(X) ;
% % concatenate into one matrix, reshape into 2D and flip columns
% Y = reshape(cat(N+1,Y{:}),[],N) ;
% else
% % no combinations have to be made
% Y = X(:) ;
% end
%%% Previous algorithms
%% Combinations using for-loops
nv = length(V) ;
M = V(:) ;
for q=1:nv^N
for w=1:N
M(q,w)= 0 ; % declaration
end
end
for ii=1:N
cc = 1 ;
for jj=1:(nv^(ii-1))
for kk=1:nv
for mm=1:(nv^(N-ii)),
M(cc,ii) = V(kk) ;
cc = cc + 1 ;
end
end
end
end
%% Version 3.2
% % COMBN is very fast using a single matrix multiplication, without any
% explicit for-loops.
% nV = numel(V) ;
% % use a math trick
% A = [0:nV^N-1]+(1/2) ;
% B = [nV.^(1-N:0)] ;
% IND = rem(floor((A(:) * B(:)')),nV) + 1 ;
% M = V(IND) ;
%% Version 2.0
% for i = N:-1:1
% X = repmat(1:nV,nV^(N-i),nV^(i-1));
% IND(:,i) = X(:);
% end
% M = V(IND) ;
%% Version 1.0
% nV = numel(V) ;
% % don waste space, if only one output is requested
% [IND{1:N}] = ndgrid(1:nV) ;
% IND = fliplr(reshape(cat(ndims(IND{1}),IND{:}),[],N)) ;
% M = V(IND) ;