Path: news.mathworks.com!newsfeed-00.mathworks.com!oleane.net!oleane!news.in2p3.fr!in2p3.fr!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!cs.uu.nl!news.stack.nl!feeds.news.ox.ac.uk!news.ox.ac.uk!feed2.jnfs.ja.net!jnfs.ja.net!gemini.csx.cam.ac.uk!news.cam.ac.uk!tpl
From: tpl@eng.cam.ac.uk (Tim Love)
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help speeding up/replacing loops
Date: 17 Jun 2009 13:06:09 GMT
Organization: University of Cambridge, England
Lines: 18
Message-ID: <h1apo1$kjc$1@gemini.csx.cam.ac.uk>
References: <h18ge7$gta$1@fred.mathworks.com> <h1ao8t$r0f$1@fred.mathworks.com>
NNTP-Posting-Host: last.eng.cam.ac.uk
Xref: news.mathworks.com comp.soft-sys.matlab:548266


"James Wright" <jameswright1001@yahoo.co.uk> writes:

>Thanks for all the help so far, really appreciate it! This is an update of what my code looks like now, which I think is quite a bit better than before, but there's still a few points I could do with some help on.

>I'm wondering how I would vectorize
I think you should worry most about the loops within loops within loops.

>B(1) = a*x*(1-x);
>for j = 2:N
>     B(j) = a*(B(j-1))*(1-(B(j-1)));
>end

Try (I think)

Bshifted=circshift(B(1:N),[1,1]);
B(1:N)=a.*Bshifted.*(1-Bshifted);
B(1) = a*x*(1-x);