How to shuffle rows in pairs?

2 ビュー (過去 30 日間)
Neuro
Neuro 2022 年 3 月 23 日
コメント済み: Neuro 2022 年 3 月 23 日
Hello. I have a 600 x 1 matrix. They can have only 8 values, let's say 1, 2, 3, 4, 5, 6, 7, and 8. I want to randomize the order of the rows but in pairs so 1 is always followed by 2, and 3 is always followed by 4, 5 is always followed by 6, and 7 is always followed by 8. Like there were only 4 elements: 1-2, 3-4, 5-6, and 7-8. But in pairs in rows (within the same column).
One case: if we have
1
2
3
4
5
6
7
8
One of the many randomizations would be:
5
6
1
2
7
8
3
4
Thank you!
  2 件のコメント
Image Analyst
Image Analyst 2022 年 3 月 23 日
I don't understand how the 600 and 8 relate to each other. So do the numbers 1-8 repeat each other for rows 9-16, 17-24, etc. up to 593-600? Do you want the shuffling to occur totally within the band, like rows 1-8 must still be in rows 1-8 and rows 593-600 must still be in rows 593-600? Or can any row end up anyplace except that the following row must also be moved so that it's following it in its new row location?
Neuro
Neuro 2022 年 3 月 23 日
Sorry. 600 is the number of rows and the integers 1 to 8 are the elements - they repeat each other. I need to shuffle the elements so they are shuffled in pairs so the element 2 is always preceeded by a 1, the 4 is always preceded by a 3, and so on. Like shuffling in pairs.

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

回答 (2 件)

Stephen23
Stephen23 2022 年 3 月 23 日
編集済み: Stephen23 2022 年 3 月 23 日
V = 1+mod(0:599,8).' % fake data
V = 600×1
1 2 3 4 5 6 7 8 1 2
M = reshape(V,2,[]);
X = randperm(size(M,2));
V = reshape(M(:,X),[],1)
V = 600×1
1 2 7 8 3 4 5 6 5 6

Arif Hoq
Arif Hoq 2022 年 3 月 23 日
編集済み: Arif Hoq 2022 年 3 月 23 日
try this:
A=randi([1 8],600,1);
for i=1:size(A,1)
if A(i)==1
A(i+1)=2;
elseif A(i)==3
A(i+1)=4;
elseif A(i)==5
A(i+1)=6;
elseif A(i)==7
A(i+1)=8;
end
end
  4 件のコメント
Jan
Jan 2022 年 3 月 23 日
I'm not sure what the purpose of your code is. A shorter version:
for i = 1:size(A,1)
if A(i) < 8
A(i+1) = A(i) + 1;
end
end
This might append an element and is not a random shuffling.
Neuro
Neuro 2022 年 3 月 23 日
Thanks, but it doesn't work. It shuffles things without following the rule (pairs of rows) and it adds one more element to the end.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by