Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

could anyone help me how to split the partition according to the number of groups present in it.

1 view (last 30 days)
I am having the result displayed as
The 5 partitions of set {1 2 3}:
{1 2 3}
{1 2} {3}
{1 3} {2}
{1} {2 3}
{1} {2} {3}
Now i want to have the partitions into two or three separate lines based on the number of groups present in it.
If no groups are not present in the partition they can be on single line.
  3 Comments
jaah navi
jaah navi on 12 Sep 2018
partitions are generated using two functions
function 1
function A = partitions(M,K)
Kflg = false; % No K passed in.
Aflg = false; % A set not passed in.
if length(M)>1
N = length(M); % User passed {'here','there','everywhere'} for example.
Aflg = true;
else
if iscell(M) % User passed {2} for example.
error('Set arguments must have more than one element.')
end
N = M;
end
if nargin>1
Kflg = true; % Used in while loop below, K passed in.
S = stirling(N,K); % The number of partitions, Stirling number.
A = cell(S,min(1,K)); % Main Cell.
cnt = 1; % Start the while loop counter.
else
K = 0; % Since user doesn't want only certain partitions.
S = ceil(sum((1:2*N).^N./cumprod(1:2*N))/exp(1)); % Bell number.
A = cell(S,1); % Main Cell.
if Aflg
A{1} = {M};
else
A{1} = {1:N}; % First one is easy.
end
cnt = 2; % Start the while loop counter.
end
if K~=ceil(K) || numel(K)~=1
error('Second argument must be an integer of length 1. See help.')
end
if N<0 || K>N || K<0
error('Arguments must be greater than zero, and K cannot exceed N.')
end
if N==0
A = {{}}; % Easy case.
return
end
if K==1
if Aflg
A{1} = {M} ; % Easy case.
else
A{1} = {1:N}; % Easy case.
end
return
end
if Aflg
NV = M;
else
NV = 1:M; % Vector: base partition, RGF indexes into this guy.
end
NU = 1; % Number of unique indices in current partition.
stp1 = N; % Controls assigning of indices.
RGF = ones(1,N); % Holds the indexes.
BLD = cell(1,N); % Smaller cell array will be used in creating larger.
while cnt<=S
idx1 = N; % Index into RGF.
stp2 = RGF(idx1); % Works with stp1.
while stp1(stp2)==1
RGF(idx1) = 1; % Assign value to RGF.
idx1 = idx1 - 1; % Need to increment idx1 for translation below.
stp2 = RGF(idx1); % And set this guy for stp1 assign below.
end
NU = NU + idx1 - N; % Get provisional number of unique vals.
stp1(1) = stp1(1) + N - idx1;
if stp2==NU % Increment the number of unique elements.
NU = NU +1;
stp1(NU) = 0;
end
RGF(idx1) = stp2 + 1; % Increment this position.
stp1(stp2) = stp1(stp2) - 1; % Translate indices of these two.
stp1(stp2+1) = stp1(stp2+1) + 1; % Re-assign stopper.
if NU==(~Kflg * NU + Kflg * K)
% We could use: C{cnt} = accumarray(RGF',NV',[],@(x) {x}); (SLOW!!)
% or the next lines to C{cnt} = TMP;
TMP = BLD(1:NU); % Build subcell of correct size.
TMP{1} = NV(RGF==1); % These first two are always here.... no loop.
TMP{2} = NV(RGF==2);
for ii = 3:NU % Build the rest of cell array, if any.
TMP{ii} = NV(RGF==ii);
end
A{cnt} = TMP; % Assign cell at jj.
cnt = cnt + 1;
end
end
function S = stirling(N,K)
% Calculate the Stirling number of the second kind. Subfunc to partitions.
for ii = K:-1:0
S(ii+1) = (-1)^ii * prod(1:K) / (prod(1:(K-ii)) *...
(prod(1:ii)))*(K - ii)^N; %#ok
end
S = 1/prod(1:K) * sum(S);
function 2
function [] = partdisp(A,K)
if nargin>2
error('A maximum of two input argument is allowed.')
end
if ~iscell(A)
error('This function is only useful on the output from PARTITIONS.')
end
L = length(A);
fprintf('\n')
if iscell(A{1}{1})
if ischar(A{1}{1}{1})
f = '%s ';
if nargin==1
fprintf('%s',['The ',num2str(L),' partitions of set {'])
for ii = 1:length(A{1})
for jj = 1:length(A{1}{ii})
fprintf('%s ',A{1}{ii}{jj})
end
end
fprintf('\b%s\n','}:')
else
fprintf('%s',['The ',num2str(L),' partitions of set {'])
for ii = 1:length(A{1})
for jj = 1:length(A{1}{ii})
fprintf('%s ',A{1}{ii}{jj})
end
end
fprintf('\b%s\n',['} of length ',num2str(K),':'])
end
else
f = '%g ';
% if nargin==1
% disp(['The ',num2str(L),' partitions of set {',...
% num2str([A{1}{1}{:}],'%g '), '}:'])
% else
% fprintf('%s',['The ',num2str(L),' partitions of set {'])
% for ii = 1:length(A{1})
% for jj = 1:length(A{1}{ii})
% fprintf('%g ',A{1}{ii}{jj})
% end
% end
% fprintf('\b%s\n',['} of length ',num2str(K),':'])
% end
end
% for ii = 1:length(A)
% for jj = 1:length(A{ii})
% fprintf('{')
for kk = 1:length(A{ii}{jj})
fprintf(f,A{ii}{jj}{kk})
end
fprintf('\b} ')
% end
% fprintf('\n')
% end
else
if ischar(A{1}{1})
f = '%s ';
if nargin==1
disp(['The ',num2str(L),' partitions of set {',[A{1}{:}],'}:'])
else
disp(['The ',num2str(L),' partitions of set {', [A{1}{:}],...
'} of length ',num2str(K),':'])
end
else
f = '%g ';
if nargin==1
disp(['The ',num2str(L),' partitions of set {',...
num2str([A{1}{:}],'%g '), '}:'])
else
disp(['The ',num2str(L),' partitions of set {',...
num2str([A{1}{:}],'%g '), '} of length ',num2str(K),':'])
end
end
for ii = 1:length(A)
for jj = 1:length(A{ii})
fprintf('{')
fprintf(f,A{ii}{jj})
fprintf('\b} ')
end
fprintf('\n')
end
end
fprintf('\n')
code:
user=[3];
for t = 1:length(user)
A = partitions([user(t)])
home;
partdisp(A);
end
I am getting the following result by running the code with respect to two functions.
The 5 partitions of set {1 2 3}:
{1 2 3}
{1 2} {3}
{1 3} {2}
{1} {2 3}
{1} {2} {3}
What i actually need is i want to execute each partition
by considering the groups present in it as per the following code .
D=[3 ]
for t = 1:length(D)
A=1:user(t)
while ~isempty(A)
B=ceil(sqrt(randi([numel(A)])))
C=A(randsample(length(A),B))
[~,idx]=find(ismember(A,C))
A(idx)=[]
end
end
The above code gives the result for one partition by considering the groups present in it.but i want to have the result for each partition.could you please help me on this.

Answers (0)

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!