x=1:block:t ; %Numbers
req = bsxfun(@plus, x(randperm(t/block)),(0:block-1).'); %generating random blocks of #
%or req=x(randperm(t/block))+(0:block-1).' ; if you have MATLAB R2016b or later
req=req(:); %reshape
其中,
t = 总数
块 = 一个块中的数字
%Sample run with t=12 and block=3
>> req.'
ans =
10 11 12 4 5 6 1 2 3 7 8 9
编辑:如果您还希望每个块中的数字以随机顺序排列,请在上述代码的最后一行之前
添加以下 3 行:
[~, idx] = sort(rand(block,t/block)); %generating indices for shuffling
idx=bsxfun(@plus,idx,0:block:(t/block-1)*block); %shuffled linear indices
req=req(idx); %shuffled matrix
%Sample run with t=12 and block=3
req.'
ans =
9 8 7 2 3 1 12 10 11 5 6 4