Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorized function iterating
Date: Tue, 18 Mar 2008 10:07:19 +0000 (UTC)
Organization: Pieper GmbH
Lines: 13
Message-ID: <fro48n$ljt$1@fred.mathworks.com>
References: <fro1g4$ibc$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1205834839 22141 172.30.248.37 (18 Mar 2008 10:07:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 18 Mar 2008 10:07:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 605234
Xref: news.mathworks.com comp.soft-sys.matlab:457784



> 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.