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

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-1-10
@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.

请先登录,再进行评论。

采纳的回答

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

更多回答(1 个)

KSSV
KSSV 2017-1-10
编辑:KSSV 2017-1-10
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 个评论
MANISH KUMAR
MANISH KUMAR 2017-1-10
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 2017-1-10
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

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by