How can I generate an array that satisfies a given number of repetitions and unique values

I have a repetition vector x = [3 2 1 2] and set of unique values y = [1 2 3 4]; How can I generate a vector z such that x = histc(z,y);
In this case z would be [1 1 1 2 2 3 4 4]
Thanks in advance!
KL

 Accepted Answer

Edit
x = [3 2 1 2]
y = [1 2 3 4]
z=arrayfun(@(ii,jj) repmat(jj,1,ii),x,y,'un',0)

5 Comments

Yes, any permutation of the elements of z would also satisfy x = histc(z,y).
If you add to z values that are not in y, you will get the same result too
This works great. It is true that any permutation works, nevertheless, I only need the sorted z.
Thanks!

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!