enigmae
enigmae

Reputation: 349

Set generation algorithm: Matlab

Consider the code :

clear all


N = 7;
Set = 1:N;
for i = 1:N-1
    Set1 = nchoosek(Set,i);
    [ L_Set1 , C_Set1] = size(Set1);
    A = zeros( L_Set1,N-C_Set1);
    ASS_R7 = zeros( L_Set1 , N+1 );
    for i1 = 1:L_Set1
        A(i1,:) = setdiff( 1:N , Set1(i1,:) );
        ASS_R7(i1,:) = [ Set1(i1,:), 0 ,A(i1,:) ];

    end
    ASS_R(i) = {ASS_R7};
end

Here, ASS_R gives all the possible [ (N-k) 0 k ] sets where the elements are unique (and belong to [1,7].Also k>0).

I have been trying to generalize this code for all N<=7 and have not been able to come up with a solution.

To be more clear: We get a cell array with cells of different sizes which look like this:

 [ 1 2 3 4 5 6 0 7 ]            . . .          [ 1 0 2 3 4 5 6 7 ]
         .                                              .
{         .         }           . . .         {         .         }  
         .                                              .
 [ 2 3 4 5 6 7 0 1 ]            . . .          [ 7 0 1 2 3 4 5 6 ] 

However, I want all cells

 [ 1 0 2 ]       [ 1 0 2 3]                                     [ 1 0 2 3 4 5 6 7 ]
     .               .                                                  .
{    .    } . . {    .     }   . . .                          {         .         }
     .               .                                                  .
 [ 7 0 6 ]       [ 7 0 6 5]                                     [ 7 0 1 2 3 4 5 6 ]


                  [ 1 2 0 3 ]                                   [ 1 2 0 3 4 5 6 7 ]
                      .                                                  .
                 {    .      }          . . .                  {         .         }
                      .                                                  . 
                  [ 6 7 0 5 ]                                   [ 6 7 0 1 2 3 4 5 ]



                                                                [ 1 2 3 4 5 6 0 7 ]            
                                                                         .                      
                                                               {         .         }             
                                                                         .                      
                                                                [ 2 3 4 5 6 7 0 1 ]  

Any ideas, guys?

Upvotes: 1

Views: 62

Answers (1)

Divakar
Divakar

Reputation: 221644

Code

%%// Array of elements whose sets are to be formed
arr1 = 1:7;

%%// Get a size estimate of the final output cell array and initialize it
lim1 = cumsum(1:numel(arr1)-1);
outmat = cell(lim1(end),1);

%%// Get the cell array of sets, into outmat
cc1=1;
for k3 = 1:numel(arr1)-1
    t1 = nchoosek(arr1,k3);
    for k2=1:numel(arr1)-k3
        mat1 =[];
        for k1 = 1:size(t1,1)
            t11 = t1(k1,:);
            t2 = arr1(~ismember(arr1,t11));
            t3 = nchoosek(t2,k2);
            t4 = [repmat([t11 0],size(t3,1),1) t3];
            mat1= [mat1; t4];
        end
        outmat(cc1)={mat1}; %%// Output
        cc1 = cc1+1;
    end
end

Upvotes: 1

Related Questions