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