MATLAB: If i have two vector how to perform single crossover

functiongenetic algorithmvector

if i have
p1 = [ 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 1]
p2 = [ 0 0 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 1 0 0]
how to perform single crossover
c1 = [ 1 0 1 1 1 0 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0 0]
c2 = [ 0 0 0 0 1 1 1 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1]

Best Answer

This is how I do it in genetic algorithm programming:
p1 = [ 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 1];
p2 = [ 0 0 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 1 0 0];
CrossoverIndex = 12; % This Would Usually Be Random
c1 = [p1(1:CrossoverIndex) p2(CrossoverIndex+1:end)];
c2 = [p2(1:CrossoverIndex) p1(CrossoverIndex+1:end)];
Producing:
c1 = 1 0 1 1 1 0 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0 0
c2 = 0 0 0 0 1 1 1 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1