How to do crossover between two matrices.

2 views (last 30 days)
Suppose we have two matrices A and B. Now we randomly select the number of a rows which corresponds to a non-zero row and exchanges the corresponding rows between two matrices.
For example
A =
[0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
B =[0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0]
Now suppose randomly we selected row number 1 and 3. so we exchange these rows between matrices A and B and the answer matrices are
C =
[0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
D =[0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0]

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 25 Oct 2016
ii = [1,3]
E = cat(3,A,B);
E(ii,:,:) = E(ii,:,end:-1:1);
CC = num2cell(E,[1,2]);
[C,D] = CC{:};

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!