MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi
Learn moreOpportunities for recent engineering grads.
Apply TodayNew to MATLAB?
MATLAB Central > MATLAB Newsreader > Preallocation in MATLAB R2009a 

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. 
"rocwoods" <rocwoods@yeah.net> wrote in message <h5c475$639$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Oleg Komarov Date: 5 Aug, 2009 16:08:02 Message: 3 of 31 
"rocwoods" <rocwoods@yeah.net> wrote in message <h5c475$639$1@fred.mathworks.com>... 
"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$1@fred.mathworks.com>... 
"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Oleg Komarov Date: 5 Aug, 2009 17:32:18 Message: 6 of 31 
"someone" <someone@somewhere.net> wrote in message <h5cekt$3nt$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Steven Lord Date: 5 Aug, 2009 17:33:24 Message: 7 of 31 

Subject: Preallocation in MATLAB R2009a From: Steven Lord Date: 5 Aug, 2009 17:36:42 Message: 8 of 31 

Subject: Preallocation in MATLAB R2009a From: Oleg Komarov Date: 5 Aug, 2009 17:41:03 Message: 9 of 31 
"Steven Lord" <slord@mathworks.com> wrote in message <h5cfv8$hl$1@fred.mathworks.com>... 
"qooroo " <qooroo@gmail.com> wrote in message <h5c9nq$fsm$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Bruno Luong Date: 5 Aug, 2009 18:08:04 Message: 11 of 31 

"Steven Lord" <slord@mathworks.com> wrote in message <h5cfp2$hes$1@fred.mathworks.com>... 
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5chq4$3nt$1@fred.mathworks.com>... 
"us " 
Subject: Preallocation in MATLAB R2009a From: Bruno Luong Date: 5 Aug, 2009 18:47:03 Message: 15 of 31 
"us " <us@neurol.unizh.ch> wrote in message <h5cikr$jh$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Yair Altman Date: 5 Aug, 2009 19:20:04 Message: 16 of 31 
BTW, this is also the case in older Matlab versions, not just 2009a or 2008b. 
"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h5cm14$i3d$1@fred.mathworks.com>... 
"Matt Fig" <spamanon@yahoo.com> wrote in message <h5cmv3$lb5$1@fred.mathworks.com>... 
"us " <us@neurol.unizh.ch> wrote in message <h5cnt2$n8p$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: James Tursa Date: 5 Aug, 2009 20:32:03 Message: 20 of 31 
I just ran some tests on WinXP R2006a through R2009a, and the difference first shows up in R2008b. I recalled that some time ago I compared building a zero matrix from scratch in a Cmex file vs the MATLAB intrinsic zeros function and got essentially the same timing result. That was back in R2007a. But now when I run the same test in R2009a I get the same huge speed difference that is showing up with the a(10000,11000)=0 method. The Cmex routine used mxCalloc to get the memory. I also tried mxMalloc and got essentially the same result. So the underlying methods used for mxMalloc and mxCalloc appear to have changed from R2008a to R2008b. Maybe MATLAB now keeps better track of which memory is already zeroed out, so calls to mxCalloc or the like don't always have to do this work because it has already been done previously. Just a guess. Seems like the zeros function is not taking advantage 
On Aug 5, 12:36 pm, "Matt Fig" <spama...@yahoo.com> wrote: 
Subject: Preallocation in MATLAB R2009a From: James Tursa Date: 5 Aug, 2009 20:43:03 Message: 22 of 31 
On a related note, I previously had tested the following (R2007a??) in a Cmex routine: 
Subject: Preallocation in MATLAB R2009a From: Bruno Luong Date: 5 Aug, 2009 20:58:03 Message: 23 of 31 
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h5cq83$7o$1@fred.mathworks.com>... 
"us " <us@neurol.unizh.ch> wrote in message <h5cnt2$n8p$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: Yair Altman Date: 5 Aug, 2009 21:14:02 Message: 25 of 31 
Nathan <ngreco32@gmail.com> wrote ... 
"us " <us@neurol.unizh.ch> wrote in message <h5crqp$elm$1@fred.mathworks.com>... 
Subject: Preallocation in MATLAB R2009a From: James Tursa Date: 6 Aug, 2009 00:20:18 Message: 27 of 31 
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5cror$ar2$1@fred.mathworks.com>... 
Thanks to all the people who are concerned about this ！ 
clear all 
Still persistent in 2012a: 
Subject: Preallocation in MATLAB R2009a From: Loren Shure Date: 28 Apr, 2014 17:09:06 Message: 31 of 31 

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.
Anyone can tag a thread. Tags are public and visible to everyone.