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:
Vectorizing a loop that depens on past information

Subject: Vectorizing a loop that depens on past information

From: Alexandre Velloso

Date: 14 Jun, 2009 19:19:02

Message: 1 of 9

Hello,

Can anybody think of a solution to the problem below?

x is a vector containing real numbers being x(1) <> 0;

I want to vectorize the following loop:


for i = 2: length( x )

        if x(i) ==0
         
              x(i) = x( i - 1);

        end
end

thanks,

Alexandre

Subject: Vectorizing a loop that depens on past information

From: Siyi

Date: 14 Jun, 2009 19:45:25

Message: 2 of 9

On Jun 14, 12:19 pm, "Alexandre Velloso" <alevell...@gmail.com> wrote:
> Hello,
>
> Can anybody think of a solution to the problem below?
>
> x is a vector containing real numbers being x(1) <> 0;
>
> I want to vectorize the following loop:
>
> for i = 2: length( x )
>
>         if x(i) ==0
>
>               x(i) = x( i - 1);
>
>         end
> end
>
> thanks,
>
> Alexandre



One solution:


y = find(x);
x1 = y(cumsum(x ~= 0))

Subject: Vectorizing a loop that depens on past information

From: Siyi

Date: 14 Jun, 2009 19:46:53

Message: 3 of 9

On Jun 14, 12:19 pm, "Alexandre Velloso" <alevell...@gmail.com> wrote:
> Hello,
>
> Can anybody think of a solution to the problem below?
>
> x is a vector containing real numbers being x(1) <> 0;
>
> I want to vectorize the following loop:
>
> for i = 2: length( x )
>
>         if x(i) ==0
>
>               x(i) = x( i - 1);
>
>         end
> end
>
> thanks,
>
> Alexandre

One solution:

y = find(x);
x1 = x(y(cumsum(x ~= 0)))

Subject: Vectorizing a loop that depens on past information

From: Bruno Luong

Date: 14 Jun, 2009 19:49:01

Message: 4 of 9

"Alexandre Velloso" <alevelloso@gmail.com> wrote in message <h13if6$6hr$1@fred.mathworks.com>...
> Hello,
>
> Can anybody think of a solution to the problem below?
>
> x is a vector containing real numbers being x(1) <> 0;
>
> I want to vectorize the following loop:
>
>
> for i = 2: length( x )
>
> if x(i) ==0
>
> x(i) = x( i - 1);
>
> end
> end
>
> thanks,
>
> Alexandre

Not sure it will be faster, but here is vectorized

    z = x==0;
    nz=find(~z);
    z=find(z);
    [trash loc]=histc(z,[nz length(x)+1]);
    x(z) = x(nz(loc))

% Bruno

Subject: Vectorizing a loop that depens on past information

From: Bruno Luong

Date: 14 Jun, 2009 19:57:01

Message: 5 of 9

Siyi <Mr.Siyi.Deng@gmail.com> wrote in message <bd9d00c3-a148-4d2a-
>
> One solution:
>
> y = find(x);
> x1 = x(y(cumsum(x ~= 0)))

Neat!

Bruno

Subject: Vectorizing a loop that depens on past information

From: Bruno Luong

Date: 14 Jun, 2009 20:19:01

Message: 6 of 9

I have just timed it, for-loop is about 4-5 time faster. I would stick with for-loop solution.

Bruno

Subject: Vectorizing a loop that depens on past information

From: Jos

Date: 14 Jun, 2009 20:25:03

Message: 7 of 9

"Alexandre Velloso" <alevelloso@gmail.com> wrote in message <h13if6$6hr$1@fred.mathworks.com>...
> Hello,
>
> Can anybody think of a solution to the problem below?
>
> x is a vector containing real numbers being x(1) <> 0;
>
> I want to vectorize the following loop:
>
>
> for i = 2: length( x )
>
> if x(i) ==0
>
> x(i) = x( i - 1);
>
> end
> end
>
> thanks,
>
> Alexandre

Use FIND and CUMSUM. For a ND-vectorized solution that can handle NaN's as well , see:
http://www.mathworks.com/matlabcentral/fileexchange/19906

Jos

Subject: Vectorizing a loop that depens on past information

From: Alexandre Velloso

Date: 14 Jun, 2009 20:40:04

Message: 8 of 9

Thank you, guys.

I agree, Bruno.

Siyi, your solution was very good and fast. It seems exactly what I was looking for.

Subject: Vectorizing a loop that depens on past information

From: Alexandre Velloso

Date: 14 Jun, 2009 21:07:01

Message: 9 of 9

Thank u, Jos.

Tags for 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