Another approach using repelem, accumarray,unique
B=[2;4;5;1;6;3];
A=[1;1;1;2;3;3;3;4;5;5;5;5;6];
counts = accumarray(A,A)./unique(A);
repelem(B,counts(B));
%// or as suggested by Divakar
%// counts = accumarray(A,1);
%// repelem(B,counts(B));
PS:repelem was introduced to . If you are using a previous version, see here R2015a
source
share