MATLAB: How to transfer a few rows randomly from a matrix A to other matrices of the set of N matrices

matricesmatrixmatrix arraymatrix manipulation

For example we have 5 matrices A,B,C,D,E and we select the best matrix based on a certain parameter and suppose it is A, then we transfer a few rows from matrix A to the corresponding rows of other matrices (B,C,D and E).
For example
A = [0 0 0 1 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 1 0 0 0 0 0
0 0 0 0 0 0 1]
B = [0 1 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 1 0 0 0 0]
C = [1 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 1 0
0 0 0 1 0 0 0]
D = [0 0 1 0 0 0 0
0 0 0 0 1 0 0
1 0 0 0 0 0 0
0 0 0 1 0 0 0
0 1 0 0 0 0 0]
E = [0 0 0 0 0 1 0
0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
1 0 0 0 0 0 0]
Now we select matrix A and transfer randomly a few rows (suppose 2nd and 4th rows) to the corresponding rows of other matrices B,C,D and E and the result should be like this..
A = [0 0 0 1 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 1 0 0 0 0 0
0 0 0 0 0 0 1]
B = [0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0]
C = [1 0 0 0 0 0 0
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
0 0 0 1 0 0 0]
D = [0 0 1 0 0 0 0
0 0 1 0 0 0 0
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 1 0 0 0 0 0]
E = [0 0 0 0 0 1 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
0 1 0 0 0 0 0
1 0 0 0 0 0 0]

Best Answer

Really, the easiest is to concatenate your matrices into a 3D array.
Assuming your ResultM is a cell array:
allmatrices = cat(3, ResultM{:});
It is then trivial to copy the rows of a page to the other pages:
selectedpage = 1; %1 for A, 2 for B, etc.
selectedrows = randperm(size(allmatrices, 1), 2); %two random rows
%copy selected rows of selected page to all pages:
allmatrices(selectedrows, :, :) = repmat(allmatrices(selectedrows, :, selectedpage), 1, 1, size(allmatrices, 3));