我有一个像A={{1} {2 3 4} {5}}
这样的单元格分区。我应该发现可以通过将某些单元格合并为一个单元格或将一个单元格拆分为某些单元格来从该分区访问所有分区。
对于分区A,结果分区为:
{{1 2 3 4}{5}}, {{1 5}{2 3 4}}, {{1} {2 3 4 5}} {{1 2 3 4 5}}
{{1}{2}{3 4}{5}}, {{1}{3}{2 4}{5}}, {1}{4}{2 3}{5}},{{1}{2 3}{4}{5}}, {{1}{2 4}{3}{5}},{{1}{2}{3 4}{5}}
我知道要通过合并操作找到可访问的分区,我应该找到| A |的所有组合k。的单元格,其中2<=k<=|A|
。
但是,我不知道如何找到所有可以通过分割形成的分区。
我该怎么做? 谢谢
答案 0 :(得分:0)
function Acc_Parts=Merge_Split(in_part)
%Returns all partitions can be accessed from the input partition
si_part=size(in_part,2); %number of cells in the input partition
Bell_Number=ceil(sum((1:2*N).^N./cumprod(1:2*N))/exp(1)); %N is the number of elements in in_part partitions
Acc_Parts=cell(Bell_Number,1);
%% merge some cells to form new cell
%should find any combination k of si_part, where 2<=k<=si_part, and combine the selected cells to form a new cell
l=1; % count the new partitions
for k=2:si_part % choose k cells of si_part cells to merge them into one cell
V=nchoosek(in_part,k); %A MATLAB function gives a matrix containing all possible combinations of the elements of input vector taken k at a time
s_V=nchoosek(si_part,k); %Number of rows of comb. (Number of the possible combinations)
celldisp(V)
for i=1:s_V
cp_part=in_part;
b1=V{i,1};
for h=1:si_part %delete this cell from cp_part
f=0;
if(b1==cell2mat(in_part(h)))
cp_part(h)=[];
f=1;
break;
else
end
end
for j=2:k
b2=V{i,j};
si_cpart=size(cp_part,2);
for h=1:si_cpart %delete this cell from cp_part
f=0;
if(b2==cell2mat(cp_part(h)))
cp_part(h)=[];
f=1;
break;
else
end
end
celldisp(cp_part)
b1=[b1 b2]; %merge cells
end
new_coal={b1};
if(isempty(cp_part)==0)
Acc_Parts{l}=[new_coal,cp_part];
else
Acc_Parts{l}=[new_coal];
end
l=l+1;
end
end
%% split a cell to some cells
for i=1:si_part
cp_part=in_part;
cp_coal=in_part{i};
si_coal=size(cp_coal,2);
if (si_coal>=2) %check all cells to find cells with 2 or more elements
cp_part(i)=[];
parts=partitions(in_part{i});
si_parts=size(parts,1);
for k=2:si_parts
if(isempty(cp_part)==0)
Acc_Parts{l}=[parts{k},cp_part];
else
Acc_Parts{l}=[parts{k}];
end
l=l+1;
end
else ;
end
end
Acc_Parts(all(cellfun('isempty',Acc_Parts),2),:) = [];
Acc_Parts
celldisp(in_part)
celldisp(Acc_Parts)
end