comparison toolboxes/FullBNT-1.0.7/KPMtools/subsets.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [T, bitv] = subsets(S, U, L, sorted, N)
2 % SUBSETS Create a set of all the subsets of S which have cardinality <= U and >= L
3 % T = subsets(S, U, L)
4 % U defaults to length(S), L defaults to 0.
5 % So subsets(S) generates the powerset of S.
6 %
7 % Example:
8 % T = subsets(1:4, 2, 1)
9 % T{:} = 1, 2, [1 2], 3, [1 3], [2 3], 4, [1 4], [2 4], [3 4]
10 %
11 % T = subsets(S, U, L, sorted)
12 % If sorted=1, return the subsets in increasing size
13 %
14 % Example:
15 % T = subsets(1:4, 2, 1, 1)
16 % T{:} = 1, 2, 3, 4, [1 2], [1 3], [2 3], [1 4], [2 4], [3 4]
17 %
18 % [T, bitv] = subsets(S, U, L, sorted, N)
19 % Row i of bitv is a bit vector representation of T{i},
20 % where bitv has N columns (representing 1:N).
21 % N defaults to max(S).
22 %
23 % Example:
24 % [T,bitv] = subsets(2:4, 2^3, 0, 0, 5)
25 % T{:} = [], 2, 3, [2 3], 4, [2 4], [3 4], [2 3 4]
26 % bitv=
27 % 0 0 0 0 0
28 % 0 1 0 0 0
29 % 0 0 1 0 0
30 % 0 1 1 0 0
31 % 0 0 0 1 0
32 % 0 1 0 1 0
33 % 0 0 1 1 0
34 % 0 1 1 1 0
35
36 n = length(S);
37
38 if nargin < 2, U = n; end
39 if nargin < 3, L = 0; end
40 if nargin < 4, sorted = 0; end
41 if nargin < 5, N = max(S); end
42
43 bits = ind2subv(2*ones(1,n), 1:2^n)-1;
44 sm = sum(bits,2);
45 masks = bits((sm <= U) & (sm >= L), :);
46 m = size(masks, 1);
47 T = cell(1, m);
48 for i=1:m
49 s = S(find(masks(i,:)));
50 T{i} = s;
51 end
52
53 if sorted
54 T = sortcell(T);
55 end
56
57 bitv = zeros(m, N);
58 bitv(:, S) = masks;