Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 10 Gen 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.

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 10 Gen 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));

Più risposte (1)

KSSV
KSSV il 10 Gen 2017
Modificato: KSSV il 10 Gen 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 Commenti
MANISH KUMAR
MANISH KUMAR il 10 Gen 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 il 10 Gen 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

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by