Filter löschen
Filter löschen

How to select two matrices randomly from a set of N matrices and then randomly select a few rows from first selected matrix and then exchange with corresponding rows of other selected matrix.

1 Ansicht (letzte 30 Tage)
For example we have 10 matrices A,B,C,D,E,F,G,h,I,J and we select randomly two matrices C and H. And then Randomly select any two rows from C and exchange with corresponding rows of H.

Antworten (2)

Thorsten
Thorsten am 25 Okt. 2016
Bearbeitet: Thorsten am 25 Okt. 2016
Q = cat(3,A,B,C,D,E,F,G,H,I,J);
kk = randperm(size(Q, 3), 2); % select 2 matrices from Q
ii = randperm(size(Q, 1), 2); % select 2 rows
Q(ii, :, kk) = Q(ii, :, fliplr(kk));

Andrei Bobrov
Andrei Bobrov am 25 Okt. 2016
For the case where the matrices have the same size:
Q = cat(3,A,B,C,D,E,F,G,H,I,J);
ii = [3,8] % C and H
n = size(Q,1);
z = 1:n;
jj = z(randperm(n,2));
M = Q(:,:,ii);
M(jj,:,:) = M(jj,:,end:-1:1);
Q(:,:,ii) = M;

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by