Path: news.mathworks.com!newsfeed-00.mathworks.com!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: avoid the growing of a vector in a loop
Date: Wed, 25 Aug 2010 13:22:12 -0500
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <i53n7f$h19$1@news.eternal-september.org>
References: <i53m2c$5pg$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 25 Aug 2010 18:25:52 +0000 (UTC)
Injection-Info: mx02.eternal-september.org; posting-host="i5J7+R9zz18sq01MGwSHxQ";
	logging-data="17449"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX188XqDb8rjkUhGnJr9FN3Xa"
User-Agent: Thunderbird 2.0.0.24 (Windows/20100228)
In-Reply-To: <HIDDEN>
Cancel-Lock: sha1:qpup+JxQsSJvwWJjzc/Lo7SzpEo=
Xref: news.mathworks.com comp.soft-sys.matlab:664977

Jose wrote:
...
> first loop
> 1. I would like to preallocate bxn to avoid the grown of the vector in 
> the loop.
> 
> second loop
> 2. I would like to extract blocks of 41 points from the vector bxn
...

bxn=zeros(1,nt-1);
>  for i=1:nt-1
>  axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns);
bxn(i)=axn;

Nota Bene:  "i" is complex i in Matlab predefined; suggest using some 
other variable name to not overload it.  I use idx as a generic index 
variable as just a suggestion.

...

> 2. extracting blocks of 41 points of the vector bxn
>  for i=1:nt
>    rw=1+((length(bxn)-41)-1)*rand(1,1);
>    xn(i,:)=bxn(round(rw):round(rw)+40);
>  end

Simply generate an array of nt rn's, scale that number to the 
length(bxn) on the array and round it.  Then the selection is simply for 
each member of that vector:vector+40

--