Try 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)
Or, instead of specifying samps
explicitly, you can specify the number of repetitions for each element of a
and use arrayfun
to compute 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)