使用Matlab生成集合的幂集(powerSet)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/idealcitier/article/details/86064125

Abstract

求集合的幂集网上有较多的方法,例如回溯法、递归法等等。在此不再进行阐述。在此使用matlab下的方法combntns,该方法介绍如下:

combntns
All possible combinations of a set of values
c = combntns(choicevec,choose) returns all combinations of the
values of the input choice vector. The size of the combinations
are given by the second input. For example, if choicevec
is [1 2 3 4 5], and choose is 2, the output is a matrix
containing all distinct pairs of the choicevec set.
The output matrix has “choose” columns and the combinatorial
“length(choicevec)-choose-‘choose’” rows. The function does not
account for repeated values, treating each entry as distinct.
As in all combinatorial counting, an entry is not paired with
itself, and changed order does not constitute a new pairing.
This function is recursive.

以生成集合[1,2,3]的幂集为例,所有的幂集为[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3,4]。

Demo

下面为具体实现方法。

clc;close all; clear;
%%
A = [1,2,3];

for n = 1: length(A)
   % combntns函数从A中生成长度为n的所有组合
    com{n} = combntns(A,n); 
end
index = 1;
for i = 1:length(com)
    temp = size(com{1,i});
    for j = 1:temp(1)
        powerSet{index,:} = com{1,i}(j,:);
        index = index +1;
    end
end

结果如下:
在这里插入图片描述

Reference

[1] http://blog.sciencenet.cn/blog-791749-666235.html
[2]

猜你喜欢

转载自blog.csdn.net/idealcitier/article/details/86064125