1 ## Copyright (C) 2010-2012 Jaroslav Hajek
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} powerset (@var{a})
21 ## @deftypefnx {Function File} {} powerset (@var{a}, "rows")
23 ## Return a cell array containing all subsets of the set @var{a}.
26 ## @seealso{unique, union, setxor, setdiff, ismember}
28 function p = powerset (a, byrows_arg)
33 if (! strcmpi (byrows_arg, "rows"))
34 error ('powerset: expecting third argument to be "rows"');
36 warning ('powerset: "rows" not valid for cell arrays');
45 a = unique (a, byrows_arg);
56 error ("powerset: not implemented for more than 32 elements");
60 b = reshape (bitunpack (uint32 (0:2^n-1)), 32, 2^n)(1:n,:);
61 ## Convert to indices and lengths.
67 p = mat2cell (a(i,:), k, columns (a));
70 p = mat2cell (a(i), 1, k);
72 p = mat2cell (a(i), k, 1);
81 %! c = sort (cellstr ({ [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]}));
82 %! p = sort (cellstr (powerset ([1, 2, 3])));