在 MATLAB 中获取所有可能的聚类

Get all possible clusterings in MATLAB

假设我有一个向量 n = 1:4

我想创建一个包含这些项目所有可能分组的单元格数组,假设分组可以是任意大小,可以有任意数量的组,但每个值必须存在于每个配置中。

这是我想创建的示例,但没有任何可能性:

possibilities = {
    {[1 2 3 4]},...
    {[1 2 3], 4},...
    {[1 2 4], 3},...
    ...
    {[1 3], [2 4]},...
    ...
    {[1 3], 2, 4},...
    {1,2,3,4}
    }

所以单元格中的每个单元格都包含值 1:4,但它们可以任意分组到任意大小的组中。唯一的限制是每个唯一的聚类只能出现一次,其中 {[1 2 3], 4} 等同于 {[2 1 3], 4}{4, [3 1 2]}.

我认为 nchoosek 函数可能有用,但老实说,我对如何以合理优雅的方式将它们组合在一起感到困惑。

您可以使用 combnk:

a = 1:4;
for k = 1:length(a) 
    C{k} = combnk(a,k); 
end

我觉得这个数学概念叫幂集。请注意,上面的示例遗漏了 empy 集。

Dan pointed me to exactly what i wanted...

为了发布问题的答案,下面是它的工作原理。

>> possibilities = partitions(4)

possibilities = 

    {1x1 cell}
    {1x2 cell}
    {1x2 cell}
    {1x2 cell}
    {1x3 cell}
    {1x2 cell}
    {1x2 cell}
    {1x3 cell}
    {1x2 cell}
    {1x2 cell}
    {1x3 cell}
    {1x3 cell}
    {1x3 cell}
    {1x3 cell}
    {1x4 cell}

>> partdisp(possibilities)

The 15 partitions of set {1 2 3 4}:
{1 2 3 4} 
{1 2 3} {4} 
{1 2 4} {3} 
{1 2} {3 4} 
{1 2} {3} {4} 
{1 3 4} {2} 
{1 3} {2 4} 
{1 3} {2} {4} 
{1 4} {2 3} 
{1} {2 3 4} 
{1} {2 3} {4} 
{1 4} {2} {3} 
{1} {2 4} {3} 
{1} {2} {3 4} 
{1} {2} {3} {4}