From: "Steve Amphlett" <Firstname.Lastname@where_I_work.com>
Path: news.mathworks.com!newsfeed-00.mathworks.com!webx
Newsgroups: comp.soft-sys.matlab
Subject: Re: growing array in Matlab?
Message-ID: <ef11420.1@webx.raydaftYaTP>
Date: Wed, 17 Aug 2005 11:42:11 -0400
References: <ddvkd9$7ar$1@ruby.cit.cornell.edu> <ef11420.0@webx.raydaftYaTP>
Lines: 51
NNTP-Posting-Host: 195.162.98.103
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Xref: news.mathworks.com comp.soft-sys.matlab:296424


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.