Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: forloop efficiency
Date: Sun, 12 Jul 2009 01:16:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 26
Message-ID: <h3bdgi$88p$1@fred.mathworks.com>
References: <h3bd1h$86h$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 1247361362 8473 172.30.248.37 (12 Jul 2009 01:16:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 12 Jul 2009 01:16:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1646679
Xref: news.mathworks.com comp.soft-sys.matlab:554725


I'm guessing it has something to do with the second command being able to do everything at once, while the frist one computes sequentially.  But I only have 2 CPUs so I'm not sure how it's parallelizing it.

also: 

clear('cap')
tic;
cap=ones(15,1);
for i=1:15
    cap(i)=2;
end
toc;

clear('cap')
tic;
cap=zeros(15,1);
for i=1:15
    cap(i)=2;
end
toc;

%%

Elapsed time is 0.000080 seconds.
Elapsed time is 0.000057 seconds.

why is it more efficient to set things up as zeros than 1s ? is it because matlab's default is to set all entries to zero when making arrays, and therefore for zeros it doesn't need to do anything while for 1s it actually has to set them ??