Rearrange any matrix Randomly with a specific sequence

1 回表示 (過去 30 日間)
Mahmoud Khadijeh
Mahmoud Khadijeh 2019 年 6 月 22 日
コメント済み: Mahmoud Khadijeh 2019 年 6 月 22 日
Hello,
I have a Matrix A like this
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]'
I want to redistribute the matrix but I want to preserve a specifc sequence which is 5 here.
I mean I need a way to redistribute each five element randomly and assign them to a new matrix
for example:
the matrix B will be like this:
B=[6 7 8 9 10 11 12 13 14 15]'
the matrix C will be like this:
C=[ 1 2 3 4 5 16 17 18 19 20]'
Is that possible in MATLAB ?
Thanks,
  4 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 6 月 22 日
It seems simple, it would be better to answer if you clearly elaborate the question?
For the following inputs
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]'
Apart from above B and C, what are other possible outputs?
Mahmoud Khadijeh
Mahmoud Khadijeh 2019 年 6 月 22 日
I just want to rearrange each five element in the matrix A randomly for example ,
If I run the code, I want the matrix A to be like this:
A=[16 17 18 19 20 1 2 3 4 5 11 12 13 14 15 6 7 8 9 10 ]'
if I run the code again, I want the matrix to be like this:
A=[1 2 3 4 5 16 17 18 19 20 6 7 8 9 10 11 12 13 14 15 ]'
regards,

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

採用された回答

infinity
infinity 2019 年 6 月 22 日
Here is an example that you can refer
a = 1:20;
b = randperm(4);
n = length(b);
for i = 1:n
c(5*(i-1)+1:5*i) = a(5*(b(i)-1)+1:5*b(i));
end
  1 件のコメント
Mahmoud Khadijeh
Mahmoud Khadijeh 2019 年 6 月 22 日
Thank you very much, that's what I want

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

その他の回答 (1 件)

TADA
TADA 2019 年 6 月 22 日
A=1:20;
blockSize = 5;
nOutputBlocks = 2;
a=reshape(A,blockSize,[]);
i=sort(reshape(randperm(size(a,2)),[],nOutputBlocks),2);
B=reshape(a(:,reshape(i',1,[])),blockSize*nOutputBlocks,[])
  1 件のコメント
Mahmoud Khadijeh
Mahmoud Khadijeh 2019 年 6 月 22 日
Thank you very much, that's works with me

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by