필터 지우기
필터 지우기

shuffle blocks of elements in vector?

조회 수: 3 (최근 30일)
Daniel Weber
Daniel Weber 2017년 12월 7일
답변: Roger Stafford 2017년 12월 7일
i have for example the following vector:
v = [1;2;3;4;5;6;7;8;9;10;11;12]
now i would like to cut them into blocks of 3 elements in a row and shuffle these blocks. the end result could look like this:
v_shuffled = [1;2;3;10;11;12;7;8;9;4;5;6]
my idea is to somehow store each block as a row vector in a matrix, shuffle these rows and then build a vector out of the shuffled matrix rows. this is what i have so far (code doesnt work)
for i=1:(length(v))/4
u(i,1)=v(4*i);
u(i,2)=v(4*i+1);
u(i,3)=v(4*i+2);
u(i,4)=v(4*i+3);
end
s = u(randperm(size(u,3)),:)
for i=1:length(v)/4
h(4*i,1)=s(i,1);
h(4*i+1,1)=s(i,2);
h(4*i+2,1)=s(i,3);
h(4*i+3,1)=s(i,4);
end
any ideas?

답변 (1개)

Roger Stafford
Roger Stafford 2017년 12월 7일
v = reshape(v,[],3);
v = reshape(v(randperm(size(v,1)),:),[],1);

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by