From: Jos <x@y.z> Path: news.mathworks.com!newsfeed-00.mathworks.com!webx Newsgroups: comp.soft-sys.matlab Subject: Re: growing array in Matlab? Message-ID: <ef11420.24@webx.raydaftYaTP> Date: Fri, 19 Aug 2005 03:41:54 -0400 References: <ddvkd9$7ar$1@ruby.cit.cornell.edu> <ef11420.0@webx.raydaftYaTP> <ef11420.1@webx.raydaftYaTP> Lines: 63 NNTP-Posting-Host: 84.81.76.244 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit Xref: news.mathworks.com comp.soft-sys.matlab:296860 Steve Amphlett wrote: > > > Steve Amphlett wrote: >> >> > <snip, ways to grow arrays... > >> Have you tried growing a cell array and then when it's full, >> cell2mat() the data out? I seem to remember someone saying here >> that >> cell arrays grow well. > > I just slapped togetha test snippet and I think it's a winner: > > a = []; > x=1; > tic > for idx=1:16000 > a = [a; sin(x)]; > end > toc > > clear b > tic > for idx=1:16000 > b(idx) = {sin(x)}; > end > c=cell2mat(b); > toc > > Elapsed time is 4.422000 seconds. > Elapsed time is 0.922000 seconds. > > And if I make x a vecor, the results are even more convincing: > > a = []; > x=(1:100)'; > tic > for idx=1:1000 > a = [a; sin(x)]; > end > toc > > clear b > tic > for idx=1:1000 > b(idx) = {sin(x)}; > end > c=cell2mat(b); > toc > > Elapsed time is 3.843000 seconds. > Elapsed time is 0.078000 seconds. % Why is this b = [] ; tic ; for idx = 1:20000, b(idx) = 2 ; end ; toc ; % (slightly) faster than b = [] ; tic ; for idx = 1:20000, b = [b ; 2] ; end ; toc % ? Jos