필터 지우기
필터 지우기

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