annotate toolboxes/FullBNT-1.0.7/KPMtools/subsets.m @ 0:cc4b1211e677 tip

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