필터 지우기
필터 지우기

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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by