annotate toolboxes/FullBNT-1.0.7/KPMtools/subsetsFixedSize.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 sub_s=subsets1(s,k)
wolffd@0 2 % SUBSETS1 creates sub-sets of a specific size from a given set
wolffd@0 3 % SS = subsets1(S, k)
wolffd@0 4 %
wolffd@0 5 % S is the given set
wolffd@0 6 % k is the required sub-sets size
wolffd@0 7 %
wolffd@0 8 % Example:
wolffd@0 9 %
wolffd@0 10 % >> ss=subsets1([1:4],3);
wolffd@0 11 % >> ss{:}
wolffd@0 12 % ans =
wolffd@0 13 % 1 2 3
wolffd@0 14 % ans =
wolffd@0 15 % 1 2 4
wolffd@0 16 % ans =
wolffd@0 17 % 1 3 4
wolffd@0 18 % ans =
wolffd@0 19 % 2 3 4
wolffd@0 20 %
wolffd@0 21 % Written by Raanan Yehezkel, 2004
wolffd@0 22
wolffd@0 23 if k<0 % special case
wolffd@0 24 error('subset size must be positive');
wolffd@0 25 elseif k==0 % special case
wolffd@0 26 sub_s={[]};
wolffd@0 27 else
wolffd@0 28 l=length(s);
wolffd@0 29 ss={};
wolffd@0 30 if l>=k
wolffd@0 31 if k==1 % Exit condition
wolffd@0 32 for I=1:l
wolffd@0 33 ss{I}=s(I);
wolffd@0 34 end
wolffd@0 35 else
wolffd@0 36 for I=1:l
wolffd@0 37 ss1=subsets1(s([(I+1):l]),k-1);
wolffd@0 38 for J=1:length(ss1)
wolffd@0 39 ss{end+1}=[s(I),ss1{J}];
wolffd@0 40 end
wolffd@0 41 end
wolffd@0 42 end
wolffd@0 43 end
wolffd@0 44 sub_s=ss;
wolffd@0 45 end