tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,18 @@
function pset = powerset(theSet)
pset = cell(size(theSet)); %Preallocate memory
%Generate all numbers from 0 to 2^(num elements of the set)-1
for i = ( 0:(2^numel(theSet))-1 )
%Convert i into binary, convert each digit in binary to a boolean
%and store that array of booleans
indicies = logical(bitget( i,(1:numel(theSet)) ));
%Use the array of booleans to extract the members of the original
%set, and store the set containing these members in the powerset
pset(i+1) = {theSet(indicies)};
end
end

View file

@ -0,0 +1,5 @@
powerset({{}})
ans =
{} {1x1 cell} %This is the same as { {},{{}} }

View file

@ -0,0 +1,5 @@
powerset({{1,2},3})
ans =
{1x0 cell} {1x1 cell} {1x1 cell} {1x2 cell} %This is the same as { {},{{1,2}},{3},{{1,2},3} }