How to do crossover between two matrices.

2 次查看(过去 30 天)
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]

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by