Thread Subject: Vectorized function iterating

Subject: Vectorized function iterating

From: Ilya

Date: 18 Mar, 2008 09:20:04

Message: 1 of 16

Hello there.

Now I have to write the following code:

for i = 2 : N
  x(i) = f( x( i - 1 ) );
end

which I want to implement without "for".

Doest anybody know If there is a function in MATLAB that
could be useful in this scenario?

TIA

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:06:42

Message: 2 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:01

Message: 3 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:01

Message: 4 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:01

Message: 5 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:02

Message: 6 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:02

Message: 7 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:02

Message: 8 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:18

Message: 9 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:19

Message: 10 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 18 Mar, 2008 10:07:19

Message: 11 of 16

> for i = 2 : N
> x(i) = f( x( i - 1 ) );
> end

If f is a recursive function, you can vectorize the loop in
the following way:

x = arrayfun(@f, 2 : N);

However, I think that this code is much less efficient than
the for loop, because f(1) is calculated N - 1 times, f(2)
N - 2 times and so on. Vectorization makes sense when
mapping a pre-calculated array to a new array.

Subject: Vectorized function iterating

From: ashipyard

Date: 18 Mar, 2008 14:33:02

Message: 12 of 16

thanks for all your suggestions. although they improved on
the for loop i used, i still was not happy with the
performance i was getting for large vectors. i therefore
coded a .mex file to do this calculation which is *much*
quicker. i will submit to the file exchange soon

Subject: Vectorized function iterating

From: Ilya

Date: 18 Mar, 2008 14:37:02

Message: 13 of 16

Yeah, ok, I get it )) Recursion rules, but I meant
without "for" not literally, I wanted to do this more
efficiently than with for.
Moreover, f depends on x, not on i, so it's not recursive,
it's just describes a discrete time dynamic system x_{k+1}
= f(x_k).
Any ideas?

Subject: Vectorized function iterating

From: ashipyard

Date: 18 Mar, 2008 14:40:19

Message: 14 of 16

"ashipyard " <andrewshipyard@hush.com> wrote in message
<frojqu$49c$1@fred.mathworks.com>...
> thanks for all your suggestions. although they improved on
> the for loop i used, i still was not happy with the
> performance i was getting for large vectors. i therefore
> coded a .mex file to do this calculation which is *much*
> quicker. i will submit to the file exchange soon

Doh! Wrong thread... the danger of having too many tabs

Subject: Vectorized function iterating

From: Ilya

Date: 18 Mar, 2008 15:15:04

Message: 15 of 16

"ashipyard " <andrewshipyard@hush.com> wrote in message
> Doh! Wrong thread... the danger of having too many tabs
already do ))

Subject: Vectorized function iterating

From: OkinawaDolphin

Date: 19 Mar, 2008 08:37:02

Message: 16 of 16

"Ilya " <ilyapoz@gmail.com> wrote in message
<frok2d$6os$1@fred.mathworks.com>...
> Yeah, ok, I get it )) Recursion rules, but I meant
> without "for" not literally, I wanted to do this more
> efficiently than with for.
> Moreover, f depends on x, not on i, so it's not
recursive,
> it's just describes a discrete time dynamic system x_
{k+1}
> = f(x_k).
> Any ideas?

If you know the number of time steps in advance, you can
pre-allocate x.

 x = zeros(N, 1);

 x(1) = start_value; % not necessary if x(1) == 0

 for n = 2 : N

  x(n) = f(x(n - 1));

 end;

If you don't know the number of time steps beforehand, x
should be a cell array.

 x = {start_value};

 while signal_is_updated

  x{end + 1} = f(x{n});

 end;

x{k + 1} = f(x{k}) and x{k} is a scalar. It is not a whole
vector, but only a component of it. Vectorization means
treating a vector as a unit, i. e. not indexing it.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com