How to do crossover between two matrices.

2 vues (au cours des 30 derniers jours)
MANISH KUMAR
MANISH KUMAR le 25 Oct 2016
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]

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 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{:};

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by