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:
avoiding growing vector inside a loop

Subject: avoiding growing vector inside a loop

From: Jose

Date: 9 Dec, 2008 15:44:02

Message: 1 of 8

Hi everyone, as you can see in the next two codes, the code 2 is more efficient
than 1, because we avoid that "a" grows inside of the loop, preallocating the vector "a" in the code 2.

nt=50000;

code 1
----------------
tic
a=0;
for i=2:nt
    a(i)=a(i-1)+5;
end
toc

code 2
-----------

tic
a=zeros(1,nt); % preallocated the "a" vector
for i=2:nt
    a(i)=a(i-1)+5;
end
toc

code 1: Elapsed time is 2.302988 seconds.
code 2. Elapsed time is 0.000782 seconds. % (much better!!!)

My question is:
there is a more efficient way to avoid the growing of d1 in this code?

cut=2;
mc=mean(deltanoise); % mean of the noise.

d1=[];

Fn=Fn(:,1:cut);
FTn=FTn(1:cut,1:cut);

for i=1:length(Fn)
    d=Fn(i,1:cut)-mc(1,1:cut);
    d1=[d1 d];
end

There some form to pre-allocated the vector d1 avoiding the growing of d1 inside the loop, and make this code more efficient?

Thanks in advance,
Jose.

Subject: avoiding growing vector inside a loop

From: Adam

Date: 9 Dec, 2008 16:09:02

Message: 2 of 8

"Jose " <jose.l.vega@gmail.com> wrote in message <ghm3o2$kop$1@fred.mathworks.com>...

> d1=[];

change this line

d1 = zeros(1, d1_length);

You can calculate what d1_length is going to be. It looks like its
N = length(FN);
N + (N-1) + (N-2) + ... + 1 = N*(N+1)/2

~Adam

Subject: avoiding growing vector inside a loop

From: Jose

Date: 9 Dec, 2008 16:38:01

Message: 3 of 8


Hi Adam, you can explain it better with the next example, i will understand it better:

Code 1 (My code)
-----------
tic

d1=[];
for i=1:20000
    d=i;
    d1=[d1 d];
end
toc

Elapsed time is 0.453692 seconds.

Please, could you re-code it in code 2,
What value of "d1_length" and "FN" I have to wite to obtain the same result than code 1, but more efficient?


code 2
---------------


tic
d1=zeros(1,d1_length)
for i=1:length(FN)
    d=i;
    d1=[d1 d];
end
toc

Thanks in advance.
Jose


















"Jose " <jose.l.vega@gmail.com> wrote in message <ghm3o2$kop$1@fred.mathworks.com>...
> Hi everyone, as you can see in the next two codes, the code 2 is more efficient
> than 1, because we avoid that "a" grows inside of the loop, preallocating the vector "a" in the code 2.
>
> nt=50000;
>
> code 1
> ----------------
> tic
> a=0;
> for i=2:nt
> a(i)=a(i-1)+5;
> end
> toc
>
> code 2
> -----------
>
> tic
> a=zeros(1,nt); % preallocated the "a" vector
> for i=2:nt
> a(i)=a(i-1)+5;
> end
> toc
>
> code 1: Elapsed time is 2.302988 seconds.
> code 2. Elapsed time is 0.000782 seconds. % (much better!!!)
>
> My question is:
> there is a more efficient way to avoid the growing of d1 in this code?
>
> cut=2;
> mc=mean(deltanoise); % mean of the noise.
>
> d1=[];
>
> Fn=Fn(:,1:cut);
> FTn=FTn(1:cut,1:cut);
>
> for i=1:length(Fn)
> d=Fn(i,1:cut)-mc(1,1:cut);
> d1=[d1 d];
> end
>
> There some form to pre-allocated the vector d1 avoiding the growing of d1 inside the loop, and make this code more efficient?
>
> Thanks in advance,
> Jose.

Subject: avoiding growing vector inside a loop

From: Adam

Date: 9 Dec, 2008 16:52:02

Message: 4 of 8

"Jose " <jose.l.vega@gmail.com> wrote in message <ghm6t9$hke$1@fred.mathworks.com>...

Posting on the bottom makes threads more readable
 
> Hi Adam, you can explain it better with the next example, i will understand it better:
>
> Code 1 (My code)
> -----------
> tic
>
> d1=[];
> for i=1:20000
> d=i;
> d1=[d1 d];
> end
> toc
>
> Elapsed time is 0.453692 seconds.
>
> Please, could you re-code it in code 2,
> What value of "d1_length" and "FN" I have to wite to obtain the same result than code 1, but more efficient?
>
>
> code 2
> ---------------
>
>
> tic
> d1=zeros(1,d1_length)
> for i=1:length(FN)
> d=i;
> d1=[d1 d];
> end
> toc
>
> Thanks in advance.
> Jose

in this simple example
d1_length = length(FN)

Then this example would be exactly the same as your original example which you proclaim to understand.

In the 3rd part of your first post, it looks like you're concatenating different lengths (at first glance).

My point is, how long will d1 be at the end?

d1 = [];
for i = 1:10
    d1 = [d1 i];
end

this will have 10 elements, correct? So you would initialize d1 with 10 elements

d1 = [];
for i = 1:10
    d1 = [d1 i i];
end

This would have 20 elements. Do you see why? So you would initialize d1 with 20 elements.

It LOOKS like
d1_length = length(FN) * mcut;

It's important that you understand why and how to verify this (for your own benefit).

Even if your code takes a long time to run, you can do length(d1) at the end to see how long it should be (as a check, not a solution).

~Adam

Subject: avoiding growing vector inside a loop

From: Jose

Date: 9 Dec, 2008 18:02:02

Message: 5 of 8






"Adam" <not.real@email.com> wrote in message <ghm7ni$1ht$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghm6t9$hke$1@fred.mathworks.com>...
>
> Posting on the bottom makes threads more readable
>
> > Hi Adam, you can explain it better with the next example, i will understand it better:
> >
> > Code 1 (My code)
> > -----------
> > tic
> >
> > d1=[];
> > for i=1:20000
> > d=i;
> > d1=[d1 d];
> > end
> > toc
> >
> > Elapsed time is 0.453692 seconds.
> >
> > Please, could you re-code it in code 2,
> > What value of "d1_length" and "FN" I have to wite to obtain the same result than code 1, but more efficient?
> >
> >
> > code 2
> > ---------------
> >
> >
> > tic
> > d1=zeros(1,d1_length)
> > for i=1:length(FN)
> > d=i;
> > d1=[d1 d];
> > end
> > toc
> >
> > Thanks in advance.
> > Jose
>
> in this simple example
> d1_length = length(FN)
>
> Then this example would be exactly the same as your original example which you proclaim to understand.
>
> In the 3rd part of your first post, it looks like you're concatenating different lengths (at first glance).
>
> My point is, how long will d1 be at the end?
>
> d1 = [];
> for i = 1:10
> d1 = [d1 i];
> end
>
> this will have 10 elements, correct? So you would initialize d1 with 10 elements
>
> d1 = [];
> for i = 1:10
> d1 = [d1 i i];
> end
>
> This would have 20 elements. Do you see why? So you would initialize d1 with 20 elements.
>
> It LOOKS like
> d1_length = length(FN) * mcut;
>
> It's important that you understand why and how to verify this (for your own benefit).
>
> Even if your code takes a long time to run, you can do length(d1) at the end to see how long it should be (as a check, not a solution).
>
> ~Adam

Adam, thanks for your long last message,
 if we want to concatenating different lengths your way looks reazonable...but, my problem is more simple than that,
only i am interested to concatenating a value of d in every loop, I explain you:
d is a distance ( a simple number), and the length of the vector d1 at the end of loop will be length(FN) (look at my first post).
For this reason, my question is, if there are other form more efficient to
initialize d1 than d1=[], mybe d1=zeros(1, length(FN)), but I think if i do it, i have to change d1=[d1 d], because it would add the initial d1 with all the zeros to my first d1 in the loop, and i am not interested in it.
What do you think about it?

Specifically, lookin at the below code, is possible to optimize this code
preallocating with d1=zeros(1,10)? instead d1=[]:

tic

d1=[];
for i=1:10
    d=i;
    d1=[d1 d];
end
toc

Result:
Elapsed time is 0.000053 seconds.

d1 =

     1 2 3 4 5 6 7 8 9 10

Thanks in advance,
Jose.

Subject: avoiding growing vector inside a loop

From: Jose

Date: 9 Dec, 2008 18:17:02

Message: 6 of 8

"Jose " <jose.l.vega@gmail.com> wrote in message <ghm3o2$kop$1@fred.mathworks.com>...
> Hi everyone, as you can see in the next two codes, the code 2 is more efficient
> than 1, because we avoid that "a" grows inside of the loop, preallocating the vector "a" in the code 2.
>
> nt=50000;
>
> code 1
> ----------------
> tic
> a=0;
> for i=2:nt
> a(i)=a(i-1)+5;
> end
> toc
>
> code 2
> -----------
>
> tic
> a=zeros(1,nt); % preallocated the "a" vector
> for i=2:nt
> a(i)=a(i-1)+5;
> end
> toc
>
> code 1: Elapsed time is 2.302988 seconds.
> code 2. Elapsed time is 0.000782 seconds. % (much better!!!)
>
> My question is:
> there is a more efficient way to avoid the growing of d1 in this code?
>
> cut=2;
> mc=mean(deltanoise); % mean of the noise.
>
> d1=[];
>
> Fn=Fn(:,1:cut);
> FTn=FTn(1:cut,1:cut);
>
> for i=1:length(Fn)
> d=Fn(i,1:cut)-mc(1,1:cut);
> d1=[d1 d];
> end
>
> There some form to pre-allocated the vector d1 avoiding the growing of d1 inside the loop, and make this code more efficient?
>
> Thanks in advance,
> Jose.
It is silly....
for i=1:10
    d=i;
    d1(i)=d;
end
toc
d1

Elapsed time is 0.000005 seconds.

d1 =

     1 2 3 4 5 6 7 8 9 10
:)

Cheers Adams.

Subject: avoiding growing vector inside a loop

From: someone

Date: 9 Dec, 2008 18:17:02

Message: 7 of 8

"Jose " <jose.l.vega@gmail.com> wrote in message <ghmbqq$6oa$1@fred.mathworks.com>...
>
>
>
>
>
> "Adam" <not.real@email.com> wrote in message <ghm7ni$1ht$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <ghm6t9$hke$1@fred.mathworks.com>...
> >
> > Posting on the bottom makes threads more readable
> >
> > > Hi Adam, you can explain it better with the next example, i will understand it better:
> > >
> > > Code 1 (My code)
> > > -----------
> > > tic
> > >
> > > d1=[];
> > > for i=1:20000
> > > d=i;
> > > d1=[d1 d];
> > > end
> > > toc
> > >
> > > Elapsed time is 0.453692 seconds.
> > >
> > > Please, could you re-code it in code 2,
> > > What value of "d1_length" and "FN" I have to wite to obtain the same result than code 1, but more efficient?
> > >
> > >
> > > code 2
> > > ---------------
> > >
> > >
> > > tic
> > > d1=zeros(1,d1_length)
> > > for i=1:length(FN)
> > > d=i;
> > > d1=[d1 d];
> > > end
> > > toc
> > >
> > > Thanks in advance.
> > > Jose
> >
> > in this simple example
> > d1_length = length(FN)
> >
> > Then this example would be exactly the same as your original example which you proclaim to understand.
> >
> > In the 3rd part of your first post, it looks like you're concatenating different lengths (at first glance).
> >
> > My point is, how long will d1 be at the end?
> >
> > d1 = [];
> > for i = 1:10
> > d1 = [d1 i];
> > end
> >
> > this will have 10 elements, correct? So you would initialize d1 with 10 elements
> >
> > d1 = [];
> > for i = 1:10
> > d1 = [d1 i i];
> > end
> >
> > This would have 20 elements. Do you see why? So you would initialize d1 with 20 elements.
> >
> > It LOOKS like
> > d1_length = length(FN) * mcut;
> >
> > It's important that you understand why and how to verify this (for your own benefit).
> >
> > Even if your code takes a long time to run, you can do length(d1) at the end to see how long it should be (as a check, not a solution).
> >
> > ~Adam
>
> Adam, thanks for your long last message,
> if we want to concatenating different lengths your way looks reazonable...but, my problem is more simple than that,
> only i am interested to concatenating a value of d in every loop, I explain you:
> d is a distance ( a simple number), and the length of the vector d1 at the end of loop will be length(FN) (look at my first post).
> For this reason, my question is, if there are other form more efficient to
> initialize d1 than d1=[], mybe d1=zeros(1, length(FN)), but I think if i do it, i have to change d1=[d1 d], because it would add the initial d1 with all the zeros to my first d1 in the loop, and i am not interested in it.
> What do you think about it?
>
> Specifically, lookin at the below code, is possible to optimize this code
> preallocating with d1=zeros(1,10)? instead d1=[]:
>
> tic
>
> d1=[];
> for i=1:10
> d=i;
> d1=[d1 d];
> end
> toc
>
> Result:
> Elapsed time is 0.000053 seconds.
>
> d1 =
>
> 1 2 3 4 5 6 7 8 9 10
>
> Thanks in advance,
> Jose.
>
>
% If you preallocate d1 properly, then there is no reason to concatenate it.

d1=[];
for i=1:10
    d=i;
    d1=[d1 d];
end

 % is the same as:

d1=zeros(1,10); % or repmat(NaN,1,10) or ones(1,10) or ...
for i=1:10
    d1(i)=i;
end

% but is more efficient.

Subject: avoiding growing vector inside a loop

From: Jose

Date: 9 Dec, 2008 18:27:01

Message: 8 of 8

Exactly someone!!!
If you prellocated it, that it is.
Thanks.
This is the definitive answer.

 d1=zeros(1,10); % or repmat(NaN,1,10) or ones(1,10) or ...
 for i=1:10
 d1(i)=i;
 end


"someone" <someone@somewhere.net> wrote in message <ghmcmu$kht$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <ghmbqq$6oa$1@fred.mathworks.com>...
> >
> >
> >
> >
> >
> > "Adam" <not.real@email.com> wrote in message <ghm7ni$1ht$1@fred.mathworks.com>...
> > > "Jose " <jose.l.vega@gmail.com> wrote in message <ghm6t9$hke$1@fred.mathworks.com>...
> > >
> > > Posting on the bottom makes threads more readable
> > >
> > > > Hi Adam, you can explain it better with the next example, i will understand it better:
> > > >
> > > > Code 1 (My code)
> > > > -----------
> > > > tic
> > > >
> > > > d1=[];
> > > > for i=1:20000
> > > > d=i;
> > > > d1=[d1 d];
> > > > end
> > > > toc
> > > >
> > > > Elapsed time is 0.453692 seconds.
> > > >
> > > > Please, could you re-code it in code 2,
> > > > What value of "d1_length" and "FN" I have to wite to obtain the same result than code 1, but more efficient?
> > > >
> > > >
> > > > code 2
> > > > ---------------
> > > >
> > > >
> > > > tic
> > > > d1=zeros(1,d1_length)
> > > > for i=1:length(FN)
> > > > d=i;
> > > > d1=[d1 d];
> > > > end
> > > > toc
> > > >
> > > > Thanks in advance.
> > > > Jose
> > >
> > > in this simple example
> > > d1_length = length(FN)
> > >
> > > Then this example would be exactly the same as your original example which you proclaim to understand.
> > >
> > > In the 3rd part of your first post, it looks like you're concatenating different lengths (at first glance).
> > >
> > > My point is, how long will d1 be at the end?
> > >
> > > d1 = [];
> > > for i = 1:10
> > > d1 = [d1 i];
> > > end
> > >
> > > this will have 10 elements, correct? So you would initialize d1 with 10 elements
> > >
> > > d1 = [];
> > > for i = 1:10
> > > d1 = [d1 i i];
> > > end
> > >
> > > This would have 20 elements. Do you see why? So you would initialize d1 with 20 elements.
> > >
> > > It LOOKS like
> > > d1_length = length(FN) * mcut;
> > >
> > > It's important that you understand why and how to verify this (for your own benefit).
> > >
> > > Even if your code takes a long time to run, you can do length(d1) at the end to see how long it should be (as a check, not a solution).
> > >
> > > ~Adam
> >
> > Adam, thanks for your long last message,
> > if we want to concatenating different lengths your way looks reazonable...but, my problem is more simple than that,
> > only i am interested to concatenating a value of d in every loop, I explain you:
> > d is a distance ( a simple number), and the length of the vector d1 at the end of loop will be length(FN) (look at my first post).
> > For this reason, my question is, if there are other form more efficient to
> > initialize d1 than d1=[], mybe d1=zeros(1, length(FN)), but I think if i do it, i have to change d1=[d1 d], because it would add the initial d1 with all the zeros to my first d1 in the loop, and i am not interested in it.
> > What do you think about it?
> >
> > Specifically, lookin at the below code, is possible to optimize this code
> > preallocating with d1=zeros(1,10)? instead d1=[]:
> >
> > tic
> >
> > d1=[];
> > for i=1:10
> > d=i;
> > d1=[d1 d];
> > end
> > toc
> >
> > Result:
> > Elapsed time is 0.000053 seconds.
> >
> > d1 =
> >
> > 1 2 3 4 5 6 7 8 9 10
> >
> > Thanks in advance,
> > Jose.
> >
> >
> % If you preallocate d1 properly, then there is no reason to concatenate it.
>
> d1=[];
> for i=1:10
> d=i;
> d1=[d1 d];
> end
>
> % is the same as:
>
> d1=zeros(1,10); % or repmat(NaN,1,10) or ones(1,10) or ...
> for i=1:10
> d1(i)=i;
> end
>
> % but is more efficient.
>

Tags for this Thread

No tags are associated with 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