我有这组号码a=[1 2 3]
。我想将此数字随机放入矩阵7 x 1中,数字1必须具有2倍,数字2必须具有3倍,数字3必须具有2倍。该顺序不是必需的。答案看起来像。b=[1 2 2 2 1 3 3]'
尝试randperm
:
a=[1 2 3];
samps = [1 1 2 2 2 3 3]; % specify your desired repeats
samps = samps(randperm(numel(samps))); % shuffle them
b = a(samps)
或者,samps
您可以为的每个元素指定重复次数,a
并用于arrayfun
计算samps
:
reps = [2 3 2];
sampC = arrayfun(@(x,y)x*ones(1,y),a,reps,'uni',0);
samps = [sampC{:}];
samps = samps(randperm(numel(samps))); % shuffle them
b = a(samps)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句