Hi.
I think you don't need this anymore, but maybe someone will searching for the solution. Here is the code:
function [childA, childB] = crossover(parentA, parentB)
n = length(parentA);
childA = zeros(1,n);
childB = zeros(1,n);
x1 = randperm(n,1);
x2 = randperm(n-x1,1)+x1;
childA(x1:x2) = parentB(x1:x2);
childB(x1:x2) = parentA(x1:x2); for i = 1:x2-x1+1
mapRelation(1:2,i)=[childA(x1+i-1), childB(x1+i-1)];
end for i = 1:x1-1
if(length(find(childA == parentA(i))) < 1)
childA(i) = parentA(i);
end
if(length(find(childB == parentB(i))) < 1)
childB(i) = parentB(i);
end
end for i = x2+1:n
if(isempty(find(childA == parentA(i),1)))
childA(i) = parentA(i);
end
if(isempty(find(childB == parentB(i),1)))
childB(i) = parentB(i);
end
end while(~isempty(find(childA == 0,1)))
mapA = mapRelation;
i = find(childA == 0,1);
v = parentA(i);
while (~isempty(find(mapA == v,1)))
[j, k] = find(mapA == v,1);
if (j == 1)
v = mapA(2,k);
else
v = mapA(1,k);
end
mapA(:,k) = [];
end
childA(i) = v;
end while(~isempty(find(childB == 0,1)))
mapB = mapRelation;
i = find(childB == 0,1);
v = parentB(i);
while (~isempty(find(mapB == v,1)))
[j, k] = find(mapB == v,1);
if (j == 1)
v = mapB(2,k);
else
v = mapB(1,k);
end
mapB(:,k) = [];
end
childB(i) = v;
end
end