Jump to content

Power set: Difference between revisions

113 bytes added ,  14 years ago
m
→‎{{header|MATLAB}}: Fixed a typo and changed a variable name so it doesn't conflict with any MATLAB keywords
m (→‎{{header|MATLAB}}: Fixed a typo)
m (→‎{{header|MATLAB}}: Fixed a typo and changed a variable name so it doesn't conflict with any MATLAB keywords)
Line 780:
 
Sets are not an explicit data type in MATLAB, but cell arrays can be used for the same purpose. In fact, cell arrays have the benefit of containing any kind of data structure. So, this powerset function will work on a set of any type of data structure, without the need to overload any operators.
<lang MATLAB>function pset = powerset(set)
 
Note: Although the keyword "set" is MATLAB function, MATLAB overrides any built-in keywords
pset = {zeros(size(set))}; %Preallocate memory
 
<lang MATLAB>function pset = powerset(settheSet)
 
pset = {zeros(size(settheSet))}; %Preallocate memory
 
%Generate all numbers from 0 to 2^(num elements of the set)-1
for i = ( 0:(2^numel(settheSet))-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(settheSet)) ));
%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) = {settheSet(indicies)};
end
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.