How to shuffle rows in pairs in a 600 x 2 matrix that only contains integers?

2 次查看(过去 30 天)
Hello. I have a 600 x 2 matrix called 'File', which only contains the following integers, in sequence:
10 1
30 2
50 1
70 8
90 3
10 5
40 6
50 2
(and the list repeats itself up to row 600).
I want to shuffle (randomize) the order of the rows in pairs, so row 2 is always preceded by row 1, row 4 is always preceded by row 3, and so on. Like each pair of rows was grouped.
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).
Thank you!

采纳的回答

Jan
Jan 2022-3-23
编辑:Jan 2022-3-23
A = [10 1; ...
30 2; ...
50 1; ...
70 8; ...
90 3; ...
10 5; ...
40 6; ...
50 2];
s = size(A);
B = reshape(A, 2, s(1)/2, s(2));
B = B(:, randperm(s(1)/2), :);
B = reshape(B, s)
B = 8×2
50 1 70 8 90 3 10 5 10 1 30 2 40 6 50 2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by