0

I have a vector using Matlab whose values are from 1 to 18, I need to perform permutations of those values considering that certain numbers cannot go together. For example:

vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]

In the conditions I have that the following numbers cannot go together:

1 and 7
1 and 8
1 and 17
1 and 18
2 and 6
2 and 7
2 and 8
2 and 17
2 and 18

So a valid permutation could be:

[1 2 4 3 5 6 7 8 9 10 11 12 13 14 15 17 16 18]

An invalid permutation could be:

[1 7 4 3 5 6 2 8 9 10 11 12 13 14 15 17 16 18] 1 and 7 together
[1 8 4 3 5 6 2 7 9 10 11 12 13 14 15 17 16 18] 1 and 8 together
[1 17 4 3 5 6 2 8 9 10 11 12 13 14 15 7 16 18] 1 and 17 together

These conditions must apply in any position of the vector. I have no ideas how to do this, if someone could give me ideas I would really appreciate it.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 2
    The only idea that comes to mind is to keep generating random permutations until one satisfies the desired criteria – Luis Mendo Mar 29 '21 at 22:43

1 Answers1

0

Taking into account a previous comment, my code generates a random permutation of your vector and checks that all these combinations are not in the permutation. If some condition is found a new permutation is generated:

vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];

not_valid = 1;
while not_valid == 1
    randpermutation = vector(randperm(18))

    not_valid = 0;
    for i = 1:length(randpermutation)-1
        if randpermutation(i) == 1 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 6 || randpermutation(i) == 6 && randpermutation(i+1) == 2
            not_valid = 1;
        end
    end
end
PedroRodriguez
  • 368
  • 2
  • 9
  • Thanks for your answer, I had thought something similar, I will verify its performance in computational complexity since I will be generating matrices of between 10,000 and 100,000 permutations. – Carlos Oliver Hernndez Monteja Mar 30 '21 at 14:54