Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help speeding up/replacing loops
Date: Wed, 17 Jun 2009 15:20:05 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 21
Message-ID: <h1b1j5$kks$1@fred.mathworks.com>
References: <h18ge7$gta$1@fred.mathworks.com> <h1ao8t$r0f$1@fred.mathworks.com> <h1apo1$kjc$1@gemini.csx.cam.ac.uk> <h1b09q$ngk$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 1245252005 21148 172.30.248.37 (17 Jun 2009 15:20:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 17 Jun 2009 15:20:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:548331


I don't see an easy vectorization of this snippet.  However, I am wondering why you have not taken the good advice to pre-allocate your arrays before the loops?  This can lead to speed increases which rival those of vectorization in some cases!  For example, put this in a file and run the file:


clear
N = 10000;

tic
B(1) = 1;  % No Pre-allocation
for ii = 2:N
    B(ii) = ii + ii.^2;
end
toc

tic
B2 = ones(1,N);  % Pre-allocation
for jj = 2:N
   B2(jj) = jj + jj.^2; 
end
toc

isequal(B,B2)