Search code examples
matlabbijection

How to generate all bijections of two sets in Matlab?


what I want to do is to do the following in the simplest way in Matlab

let's suppose we have two arrays {1,2,3} {4,5,6}.

The algorithm should give me all bijections:

1-4 2-5 3-6 / 1-4 2-6 3-5 / 1-5 2-4 3-6 / 1-5 2-6 3-4 / 1-6 2-5 3-4 / 1-6 2-4 3-5


Solution

  • Create a 3D matrix using perms as suggested by trutheality and repmat to duplicate the first matrix:

    x = [1 2 3];
    y = [4 5 6];
    
    Y = perms(y);
    X = repmat(x,length(perms(y)),1);
    
    Result = cat(3,X,Y);
    NicerResult = permute(Result, [2, 3, 1]);