Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Repeating the elements of vector

Subject: Repeating the elements of vector

From: stani S.

Date: 6 Jan, 2011 16:08:05

Message: 1 of 4

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!

Subject: Repeating the elements of vector

From: Oleg Komarov

Date: 6 Jan, 2011 16:16:06

Message: 2 of 4

"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

Subject: Repeating the elements of vector

From: Ade

Date: 6 Jan, 2011 16:32:04

Message: 3 of 4

"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)

Subject: Repeating the elements of vector

From: Sean de

Date: 6 Jan, 2011 16:41:04

Message: 4 of 4

"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!

idx([cumsum([1 b(b>0)])]) = 1;
c = a(cumsum(idx(1:find(idx,1,'last')-1)));

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us