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

1 vue (au cours des 30 derniers jours)
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]
  1 commentaire
Stephen23
Stephen23 le 10 Jan 2017
@MANISH KUMAR: you should probably read this:
Most likely your code would be much simpler, faster, and more robust if you put all of your data into one array (e.g. an ND array or cell array) rather than in lots or separate variables.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 10 Jan 2017
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));

Plus de réponses (1)

KSSV
KSSV le 10 Jan 2017
Modifié(e) : KSSV le 10 Jan 2017
Let A,B,C,D,E be your matrices.
[nx,ny] = size(A) ;
rows = 1:nx ;
% select two random rows
idx = randsample(rows,2) ;
% put these rows into other matrices
B(idx,:) = A(idx,:) ;
C(idx,:) = A(idx,:) ;
D(idx,:) = A(idx,:) ;
E(idx,:) = A(idx,:) ;
  2 commentaires
MANISH KUMAR
MANISH KUMAR le 10 Jan 2017
Dear @KSSV Sir, This code is working excellent But my matrices are stored under 'ResultM' which gives all the five matrices. How to copy rows from a matrix to other matrices of 'ResultM'?
KSSV
KSSV le 10 Jan 2017
A = rand(5,7) ;
B = rand(5,7,5) ;
[nx,ny] = size(A) ;
rows = 1:nx ;
% se;lect two random rows
idx = randsample(rows,2) ;
% put these rows into other matrices
for p = 1:size(B,3)
B(idx,:,p) = A(idx,:) ;
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical 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