How to exchange a few rows randomly between matrices A and B (matrix B is selected randomly from a set of N matrices)?

2 ビュー (過去 30 日間)
MANISH KUMAR
MANISH KUMAR 2017 年 1 月 16 日
再開済み: KSSV 2017 年 1 月 16 日
For example we have ‘ResultM’ which gives 5 matrices A, B, C, D, E and we have to exchange a few random rows between matrix A and any other matrix B selected randomly from these 5 matrices. First we select a matrix randomly from 5 matrices and then we select a few rows randomly and exchange rows between these matrices (matrix A and matrix selected randomly). Same process is followed for all the matrices of the set.
Eg.row exchange operation between matrix A and randomly selected matrix
then between matrix B and randomly selected matrix
then between matrix C and randomly selected matrix
then between matrix D and randomly selected matrix
then between matrix E and randomly selected matrix
A new random matrix is chosen for each row exchange operation.
For example we have matrix A
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]
And we randomly choose matrix D (say)
D = [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]
Then we select a few rows randomly (say 2 and 4) and exchange.
The resulting matrices are
A = [0 0 0 1 0 0 0
1 0 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 0 1 0 0
0 0 0 0 0 0 1]
D = [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]
Similarly, we perform row exchange operation for all the matrices of the set.
  2 件のコメント
KSSV
KSSV 2017 年 1 月 16 日
@Stephen Cobeldick
I remember this question from Manish Kumar. I didn't close it and answered the question because there is an extra step involved in this present question. I thought it would be helpful for the user.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2017 年 1 月 16 日
編集済み: KSSV 2017 年 1 月 16 日
A = rand(5,7,5) ; % your A,B,C,D and E matrices in 3D
A0 = A ;
[m,n,p] = size(A) ;
for i = 1:p
idx = randperm(p,2) ; % selecte two rows randomly
matrices = randperm(p,1) ; % select one matrix randomly
A(idx,:,matrices) = A(idx,:,i) ; % replace random rows in selected matrix
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by