MATLAB: Shuffle a vector of repeated numbers so the numbers do not repeat

shufflevector

Hi,
I have a vector fam1_1 = [1,1,1,2,2,2,3,3,3,4,4,4] that I need shuffled without having any numbers be repeated. Does anyone have any suggestions on how to do this?

Best Answer

Edit
fam1_1 = [1,1,1,2,2,2,3,3,3,4,4,4]
ii=unique(fam1_1)
n=numel(fam1_1)
m=numel(ii)
p=numel(fam1_1)/numel(ii)
x=ii(randperm(m))
out=x
for k=2:p
y=setdiff(ii,x(end))
y1=y(randi(m-1))
y2=setdiff(ii,y1)
x=[y1 y2(randperm(m-1))]
out=[out x]
end