# HG changeset patch # User Jordi Gutiérrez Hermoso # Date 1354129496 18000 # Node ID 0f924f0b0be306725e758d96054ad4180c911b8d # Parent a1b6342403520a6a13e63bbd17daa550e37f5cac# Parent 9671baab36c409a4e6a59bd4566ba511cc7a2827 maint: merge together Carn̈́ë's changes diff -r a1b634240352 -r 0f924f0b0be3 scripts/set/powerset.m --- a/scripts/set/powerset.m Wed Nov 28 06:01:09 2012 +0100 +++ b/scripts/set/powerset.m Wed Nov 28 14:04:56 2012 -0500 @@ -19,11 +19,17 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} powerset (@var{a}) ## @deftypefnx {Function File} {} powerset (@var{a}, "rows") +## Compute the powerset (all subsets) of the set @var{a}. ## -## Return a cell array containing all subsets of the set @var{a}. +## The set @var{a} must be a numerical matrix or a cell array of strings. The +## output will always be a cell array of either vectors or strings. ## +## With the optional second argument @code{"rows"}, each row of the set @var{a} +## is considered one element of the set. As a result, @var{a} must then be a +## numerical 2D matrix. +## +## @seealso{unique, union, setxor, setdiff, ismember} ## @end deftypefn -## @seealso{unique, union, setxor, setdiff, ismember} function p = powerset (a, byrows_arg) @@ -31,7 +37,7 @@ if (nargin == 2) if (! strcmpi (byrows_arg, "rows")) - error ('powerset: expecting third argument to be "rows"'); + error ('powerset: expecting second argument to be "rows"'); elseif (iscell (a)) warning ('powerset: "rows" not valid for cell arrays'); else @@ -40,6 +46,9 @@ elseif (nargin != 1) print_usage (); endif + if (iscell (a) && ! iscellstr (a)) + error ("powerset: cell arrays can only used for character strings"); + endif if (byrows) a = unique (a, byrows_arg); @@ -50,7 +59,7 @@ endif if (n == 0) - p = []; + p = {}; else if (n > 32) error ("powerset: not implemented for more than 32 elements"); @@ -77,8 +86,12 @@ endfunction -%!test +%!shared c, p %! c = sort (cellstr ({ [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]})); %! p = sort (cellstr (powerset ([1, 2, 3]))); -%! assert (p, c); +%!assert (p, c); +%! c = sort (cellstr ({ [], [1:3], [2:4], [3:5], [1:3; 2:4], [1:3; 3:5], [2:4; 3:5], [1:3; 2:4; 3:5]})); +%! p = sort (cellstr (powerset ([1:3;2:4;3:5], "rows"))); +%!assert (p,c); +%!assert (powerset([]), {}); # always return a cell array