shuffle blocks of elements in vector?

2 ビュー (過去 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 Center および File ExchangeSimulink Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by