annotate _FullBNT/KPMtools/subsets.m @ 9:4ea6619cb3f5 tip

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