Question

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

Was it helpful?

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]);

OTHER TIPS

This is equivalent to getting all permutations of the second array, and there is a very convenient function, perms, for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top