Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Preallocation in MATLAB R2009a
Date: Wed, 5 Aug 2009 17:32:18 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 51
Message-ID: <h5cfn2$dh7$1@fred.mathworks.com>
References: <h5c475$639$1@fred.mathworks.com> <h5cap2$6e9$1@fred.mathworks.com> <h5cekt$3nt$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 1249493538 13863 172.30.248.37 (5 Aug 2009 17:32:18 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Aug 2009 17:32:18 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1886545
Xref: news.mathworks.com comp.soft-sys.matlab:561071


"someone" <someone@somewhere.net> wrote in message <h5cekt$3nt$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$1@fred.mathworks.com>...
> > "rocwoods" <rocwoods@yeah.net> wrote in message <h5c475$639$1@fred.mathworks.com>...
> > > It's known that to use a matrix variable "a" with changeless size m,n,we should preallocate memory for the variable to achieve good performance.Usually,We use "a= zeros(m,n)" to preallocate.
> > > But I find in R2009a using "a(m,n) = 0" to preallocate is much fast than "a = zeros(m,n)",especially with large m,n. The following shows this:
> > > >> clear
> > > >> tic;a = zeros(10000,11000);toc
> > > Elapsed time is 1.054111 seconds.
> > > >> clear
> > > >> tic;a (10000,11000) = 0;toc
> > > Elapsed time is 0.000049 seconds.
> > > 
> > > I think in R2009a,"a(m,n) = 0" do NOT really assign each element of a with 0,but just allocate a certain zone in memory for a.When you visit a certain element of a,the assignment happens,  is that right? Thanks to all.
> > > 
> > > Bye the way,
> > > My computer's  disposition:  Intel Core2 Duo CPU T8100 @ 2.1GHz 2.1GHz,   2G memory .
> > 
> > R2008b:
> > tic
> >     a = zeros(10000,10000);
> > toc
> > tic 
> >     b(10000,10000) = 0;
> > toc
> > isequal(a,b)
> > 
> > Elapsed time is 0.493878 seconds.
> > Elapsed time is 0.002202 seconds.
> > ans =  1
> 
> I would like to make one observation here.
> If you assume that b in the above code will
> result in matrix filled with zeros, then you are
> relying on an "undocumented" feature.
> 
> Whos to say what might happen in future MATLAB releases?
> Maybe b will be filled with NaNs execpt for b(10000,10000).
> 
> > 
> > BTW is it better to preallocate zeros or NaN? 

a = ones(2,2)
b(2,2) = 1
a =
     1     1
     1     1
b =
     0     0
     0     1

Lets say now it makes sense. has Yair Altman ever mentioned this?