"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ig4ps6$sdj$1@fred.mathworks.com>...
> "stani S." wrote in message <ig4pd5$pb0$1@fred.mathworks.com>...
> > Hi,
> >
> > Let's say I have one vector that presents some values of interest, i.e.:
> > a = [4 11 7 5 6])
> > and the other vector which tells me how many times I need to repeat each element of the first vector, i.e:
> > b = [3 3 2 1 0]
> >
> > so the resulting vector c looks like:
> > c = [4 4 4 11 11 11 7 7 5]
> >
> > I wrote the code with for loops that does that but it is very slow. Is there any faster way to do this?
> >
> > Many Thanks!
>
> Yes, run length decoding:
> a = [4 11 7 5 6];
> b = [3 3 2 1 0];
>
> c = rude(b,a) % on FEX: http://www.mathworks.com/matlabcentral/fileexchange/6436
>
> Check out the code of rude if you wanna learn how to do it in a vectorized way.
>
> Oleg
another option is this:
C = arrayfun(@(x, y) repmat(x, [1 y]), a,b, 'UniformOutput', false);
answer = cell2mat(C)
