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