1

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?

2
  • You want cell arrays of all possible sequences that can be generated? Commented Apr 14, 2014 at 6:39
  • More in the fashion I've mentioned. Like elements before and after the '0' should be unique. Also, I don't want permutations like [ 1 2 0 3 ] and [ 2 1 0 3 ]. The 0 acts like a delimiter between two sets who have empty intersection. Commented Apr 14, 2014 at 6:45

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I came with my own code but this is seems more efficient (+1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.