MATLAB: Creating a vector from a bin count

vector

Hello,
I have a matrix I got from another source that look like:
[0 5; 1 2; 2 1]
I wish to get a vector like [0 0 0 0 0 1 1 2] .
Thanks
EC

Best Answer

  • R2015a or later:
    >> M = [0 5; 1 2; 2 1];
    >> repelem(M(:,1),M(:,2)).'
    ans =
    0 0 0 0 0 1 1 2
    R2014b or earlier:
    >> C = arrayfun(@(x,n)repmat(x,1,n),M(:,1),M(:,2),'uni',0);
    >> [C{:}]
    ans =
    0 0 0 0 0 1 1 2