% MS-A050x L1B example 2 % % argument: n = number of positions (default=4) % function l1b2sequences(n) if nargin<1, n=4; end if n==1, error('sorry, this code does not work for n=1'); end fprintf('all possible 01-sequences of length %d:\n', n) totalseq = 0; for k=0:n fprintf('--------------------------------------\n'); CC = nchoosek(1:n, k); nseq = size(CC,1); totalseq = totalseq + nseq; for i=1:nseq C = CC(i,:); x = zeros(1,n); x(C) = 1; fprintf(' %d', x); if i == nseq fprintf(' %4d sequences that have %d ones', nseq, k); end fprintf('\n'); end end fprintf('--------------------------------------\n'); fprintf(repmat(' ', 1, n)); fprintf(' %4d = 2^%d sequences total\n', totalseq, n); end