MATLAB: If i have tow matrix how to crossover of them

crossoverfunctiongenetic algorithm

if i have this matrix
A = [ 1 0 1 1
1 1 0 1
0 1 0 1
1 1 1 0]
B = [ 0 1 1 0
0 0 1 1
1 1 0 1
1 1 0 0]
i want to make them like that
A' = [ 1 0 1 1 1 1 0 1 0 1 0 1 1 1 1 0 ]
B' = [ 0 1 1 0 0 0 1 1 1 1 0 1 1 1 0 0 ]
then i want to randomly crossover in which every 4 element like a group
C1 = [ 1 0 1 1 0 0 1 1 0 1 0 1 1 1 0 0 ]
C2 = [ 0 1 1 0 1 1 0 1 1 1 0 1 1 1 1 0 ]
THEN I WANT TO return C1 and C2 like this
C1 = [ 1 0 1 1
0 0 1 1
0 1 0 1
1 1 0 0 ]
C2 = [ 0 1 1 0
1 1 0 1
1 1 0 1
1 1 1 0 ]

Best Answer

R = repmat(randi([0,1],4,1),1,4);
C1 = A.*R+B.*(1-R);
C2 = A.*(1-R)+B.*R;