Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
avoid the growing of a vector in a loop

Subject: avoid the growing of a vector in a loop

From: Jose

Date: 25 Aug, 2010 18:06:04

Message: 1 of 6

Hello to everyone, I've got the next piece of code, and I want to extract
the noise (data without spikes) from my data:

ns=20;
maxindex=maximum of the spike waveform

data with spikes =data(maxindex(i)-2*ns:maxindex(i)+2*ns)..spike waveform length

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

tic

1. building the vector bxn without spikes
  bxn=[];
  for i=1:nt-1
  axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns);
  bxn=[bxn axn];
  end
  
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

  toc

To do this operation it takes 0.003sec and I would like to improve it...any help?

Thanks.

Subject: avoid the growing of a vector in a loop

From: dpb

Date: 25 Aug, 2010 18:22:12

Message: 2 of 6

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

--

Subject: avoid the growing of a vector in a loop

From: Jose

Date: 25 Aug, 2010 18:49:04

Message: 3 of 6

dpb <none@non.net> wrote in message <i53n7f$h19$1@news.eternal-september.org>...
> 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
>
> dbp, in the first loop you can't do it: bxn(i)=axn, because bxn(i) is an element of a vector, and axn is a vector.

Matlab error:
??? In an assignment A(I) = B, the number of elements in B and
 I must be the same.

Subject: avoid the growing of a vector in a loop

From: dpb

Date: 25 Aug, 2010 19:05:30

Message: 4 of 6

Jose wrote:
> dpb <none@non.net> wrote in message
> <i53n7f$h19$1@news.eternal-september.org>...
>> Jose wrote:
>> ...
>> > first loop
>> > 1. I would like to preallocate bxn to avoid the grown of the vector
>> in > the loop.
...
>> bxn=zeros(1,nt-1);
>> > for i=1:nt-1
>> > axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns);
...
>> dbp, in the first loop you can't do it: bxn(i)=axn, because bxn(i) is
>> an element of a vector, and axn is a vector.
...
oops, didn't read it carefully enough.

OK, you'll have to precompute the overall length of bxn based on the
values in maxindex to do a preallocation and then write it sotoo of
jdx1 = 1;
%begin loop
idx1 = maxindex(i)+2*ns;
idx2 = maxindex(i+1)-2*ns;
jdx2 = jdx1+idx2-idx1-1;
bxn(jdx1:jdx2)=data(idx1:idx2);
jdx1 = jdx2+1;

...

Whether you'll gain anything will depend on how large the array and loop
are as to how many extra allocations are made as opposed to the
additional overhead to compute the size a priori and the intermediate
indices.

The above could undoubtedly be optimized as well -- certainly there's at
least on common factor of 2*ns still there as well as whatever structure
features of the characteristics of maxindiex() might be, etc., etc.,
etc., ...

--

Subject: avoid the growing of a vector in a loop

From: Jose

Date: 25 Aug, 2010 19:20:20

Message: 5 of 6

dpb <none@non.net> wrote in message <i53pol$a3u$1@news.eternal-september.org>...
> Jose wrote:
> > dpb <none@non.net> wrote in message
> > <i53n7f$h19$1@news.eternal-september.org>...
> >> Jose wrote:
> >> ...
> >> > first loop
> >> > 1. I would like to preallocate bxn to avoid the grown of the vector
> >> in > the loop.
> ...
> >> bxn=zeros(1,nt-1);
> >> > for i=1:nt-1
> >> > axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns);
> ...
> >> dbp, in the first loop you can't do it: bxn(i)=axn, because bxn(i) is
> >> an element of a vector, and axn is a vector.
> ...
> oops, didn't read it carefully enough.
>
> OK, you'll have to precompute the overall length of bxn based on the
> values in maxindex to do a preallocation and then write it sotoo of
> jdx1 = 1;
> %begin loop
> idx1 = maxindex(i)+2*ns;
> idx2 = maxindex(i+1)-2*ns;
> jdx2 = jdx1+idx2-idx1-1;
> bxn(jdx1:jdx2)=data(idx1:idx2);
> jdx1 = jdx2+1;
>
> ...
>
> Whether you'll gain anything will depend on how large the array and loop
> are as to how many extra allocations are made as opposed to the
> additional overhead to compute the size a priori and the intermediate
> indices.
>
> The above could undoubtedly be optimized as well -- certainly there's at
> least on common factor of 2*ns still there as well as whatever structure
> features of the characteristics of maxindiex() might be, etc., etc.,
> etc., ...
>
> --

yes, we did the same:
Here you are mine:

 bxn=zeros(nt,1000);
  ini=0;
  for i=1:nt-1
  axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns)
  bxn(ini+1:ini+length(axn))=axn
  ini=length(axn);
  end

Thanks.

Subject: avoid the growing of a vector in a loop

From: Roger Stafford

Date: 25 Aug, 2010 20:34:05

Message: 6 of 6

"Jose " <jose.l.vega@gmail.com> wrote in message <i53m2c$5pg$1@fred.mathworks.com>...
> Hello to everyone, I've got the next piece of code, and I want to extract
> the noise (data without spikes) from my data:
>
> ns=20;
> maxindex=maximum of the spike waveform
>
> data with spikes =data(maxindex(i)-2*ns:maxindex(i)+2*ns)..spike waveform length
>
> 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
>
> tic
>
> 1. building the vector bxn without spikes
> bxn=[];
> for i=1:nt-1
> axn=data(maxindex(i)+2*ns:maxindex(i+1)-2*ns);
> bxn=[bxn axn];
> end
>
> 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
>
> toc
>
> To do this operation it takes 0.003sec and I would like to improve it...any help?
>
> Thanks.
- - - - - - - - - -
  On your first loop it is inadvisable to append axn repeatedly onto bxn a large number of times. That could cost heavily in cpu time if maxindex is large. You can do the following without a for-loop instead. You can run time tests to see if there is an advantage.

 bxn = zeros(1,size(data,2)+4*ns+1);
 bxn(maxindex) = 1;
 bxn(maxindex+4*ns+1) = bxn(maxindex+4*ns+1) - 1;
 bxn = cumsum(bxn);
 bxn = data(bxn(2*s+1:end-2*s-1)==0);

This assumes there are no repetitions among the indices in maxindex and that its indices all lie within the range of data.

Roger Stafford

Tags for this Thread

What are tags?

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.

Contact us