使用Matlab生成集合的冪集 powerSet

2021-09-07 23:26:56 字數 1440 閱讀 9065

求集合的冪集網上有較多的方法,例如回溯法、遞迴法等等。在此不再進行闡述。在此使用matlab下的方法combntns,該方法介紹如下:

combntnsall 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]。

下面為具體實現方法。

clc;close all; clear;

%%a = [1,2,3];

for n = 1: length(a)

% combntns函式從a中生成長度為n的所有組合

com = combntns(a,n);

endindex = 1;

for i = 1:length(com)

temp = size(com);

for j = 1:temp(1)

powerset = com(j,:);

index = index +1;

endend

結果如下:

reference

[1]

[2]

求集合的冪集

集合的冪集a 則a的冪集為 對於求a集合的冪集,a中的元素它只有兩中狀態,它或屬於冪集的元素集,或者不屬於冪集的元素集。求冪集的過程可以看成是對a中元素進行 取 或 舍 的過程。狀態樹如下 葉子節點表示終結狀態,而第i層的分支節點,則表示已對集合a中前i 1個元素進行了取 舍處理的狀態。求冪集的過程...

求集合的冪集

這是朋友叫我幫他寫的,足足幹了兩天,終於在養好精神後把它做出來了,雖然是遞迴實現的,但總算是弄出來了,兩天時間裡搞了好多遍迴圈來實現,但是寫著寫著就迷糊了,洗了個頭,刷了把臉,人精神了,換了遞迴刺溜一下就寫出來了,手動開心 哈哈哈哈。但是據說還可以用分治法 回溯法 窮舉法來實現哦 include i...

集合的冪集dfs stack

思路n n 20 的值以及s的n個元素 s的冪集 3 abc c b bc a ac ab abc include include include include using namespace std int c 0 stackss void dfs char str int n,int i if...