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 for loop

Subject: Avoiding for loop

From: Stefan

Date: 23 Jul, 2010 19:10:24

Message: 1 of 14


for x1 = -10:0.1:10;
    for x2 = -10:0.1:10;
        for x3 = -10:0.1:10;
            for x4 = -10:0.1:10;

                    V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
            end
        end
     end
end

Subject: Avoiding for loop

From: Stefan

Date: 23 Jul, 2010 19:18:07

Message: 2 of 14

Hi,
Can someone help me to make this piece of code more efficient?
Thanks

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2cpf0$j3k$1@fred.mathworks.com>...
>
> for x1 = -10:0.1:10;
> for x2 = -10:0.1:10;
> for x3 = -10:0.1:10;
> for x4 = -10:0.1:10;
>
> V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
> end
> end
> end
> end

Subject: Avoiding for loop

From: Stefan

Date: 23 Jul, 2010 19:21:21

Message: 3 of 14

Hi,
Can someone help me to make this piece of code more efficient?
Thanks

Here A =[1 2;2;1];
B = [4 2;2;5];

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2cpf0$j3k$1@fred.mathworks.com>...
>
> for x1 = -10:0.1:10;
> for x2 = -10:0.1:10;
> for x3 = -10:0.1:10;
> for x4 = -10:0.1:10;
>
> V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
> end
> end
> end
> end

Subject: Avoiding for loop

From: Walter Roberson

Date: 23 Jul, 2010 19:21:19

Message: 4 of 14

Stefan wrote:
>
> for x1 = -10:0.1:10;

Before this statement we already had enough information to calculate expm(B)

> for x2 = -10:0.1:10;

After that statement we have enough information to calculate

[x1;x2]'*A*[x1;x2]

> for x3 = -10:0.1:10;
> for x4 = -10:0.1:10;
> V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]

No need to do all those calculations here: as indicated above some of this can
be pre-calculated.

> end
> end
> end
> end

After using the above hints, you might find ways to vectorize more of the
calculations.

Subject: Avoiding for loop

From: Roger Stafford

Date: 23 Jul, 2010 19:32:21

Message: 5 of 14

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2cpf0$j3k$1@fred.mathworks.com>...
>
> for x1 = -10:0.1:10;
> for x2 = -10:0.1:10;
> for x3 = -10:0.1:10;
> for x4 = -10:0.1:10;
>
> V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
> end
> end
> end
> end
- - - - - - - - -
  What is it you wish to do with those 1,632,240,801 V's after you have computed them?

Roger Stafford

Subject: Avoiding for loop

From: Stefan

Date: 23 Jul, 2010 19:39:09

Message: 6 of 14

thx for these remarks.
But i don't see how to do what you suggested. I also have [x1;x2]'*expm(B)*[x3;x4] to compute.

Walter Roberson <roberson@hushmail.com> wrote in message <i2cqal$ra0$1@canopus.cc.umanitoba.ca>...
> Stefan wrote:
> >
> > for x1 = -10:0.1:10;
>
> Before this statement we already had enough information to calculate expm(B)
>
> > for x2 = -10:0.1:10;
>
> After that statement we have enough information to calculate
>
> [x1;x2]'*A*[x1;x2]
>
> > for x3 = -10:0.1:10;
> > for x4 = -10:0.1:10;
> > V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
>
> No need to do all those calculations here: as indicated above some of this can
> be pre-calculated.
>
> > end
> > end
> > end
> > end
>
> After using the above hints, you might find ways to vectorize more of the
> calculations.

Subject: Avoiding for loop

From: Steven_Lord

Date: 23 Jul, 2010 19:50:17

Message: 7 of 14



"Stefan " <aidematlab@yahoo.ca> wrote in message
news:i2cr4t$7oq$1@fred.mathworks.com...
> thx for these remarks.
> But i don't see how to do what you suggested. I also have
> [x1;x2]'*expm(B)*[x3;x4] to compute.

Does expm(B) depend on any of x1, x2, x3, or x4? If not, why compute it
inside those loops?

You're computing that quantity 201^4 times -- you only need to calculate it
once, and then you can USE it as often as you need to use it. That's what
Walter was alluding to.

Anyway, related to Roger's question later in the thread -- do you actually
need all 201^4 values of V or is the code correct and you only need the
value of V corresponding to the situation where all of x1, x2, x3, and x4
are equal to 10? If the latter, only calculate that one value of V and your
computation will be complete MUCH, MUCH more quickly.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Avoiding for loop

From: Andy

Date: 23 Jul, 2010 19:58:23

Message: 8 of 14

You could rewrite it as:

B=expm(B); % so you calculate this only once, right at the start
for x1 = -10:0.1:10;
    for x2 = -10:0.1:10;
        tmp1 = [x1;x2]'*A*[x1;x2];
        tmp2 = [x1;x2]'*B;
        for x3 = -10:0.1:10;
            for x4 = -10:0.1:10;
                    V =tmp1 + tmp2*[x3;x4]; % <- note the added semicolon
            end
        end
     end
end

But keep in mind that you overwrite V every time, as Roger points out. So why don't you tell us what you're actually trying to do? Then there may be a way to remove the for loops entirely.

Subject: Avoiding for loop

From: Stefan

Date: 23 Jul, 2010 20:04:07

Message: 9 of 14

You are both right, compute expm(B) outside the loop first. However, I need all the values of V. -a<= x1,x2,x3,x4 <=a, where a can be any integer.

"Steven_Lord" <slord@mathworks.com> wrote in message <i2crpp$jcj$1@fred.mathworks.com>...
>
>
> "Stefan " <aidematlab@yahoo.ca> wrote in message
> news:i2cr4t$7oq$1@fred.mathworks.com...
> > thx for these remarks.
> > But i don't see how to do what you suggested. I also have
> > [x1;x2]'*expm(B)*[x3;x4] to compute.
>
> Does expm(B) depend on any of x1, x2, x3, or x4? If not, why compute it
> inside those loops?
>
> You're computing that quantity 201^4 times -- you only need to calculate it
> once, and then you can USE it as often as you need to use it. That's what
> Walter was alluding to.
>
> Anyway, related to Roger's question later in the thread -- do you actually
> need all 201^4 values of V or is the code correct and you only need the
> value of V corresponding to the situation where all of x1, x2, x3, and x4
> are equal to 10? If the latter, only calculate that one value of V and your
> computation will be complete MUCH, MUCH more quickly.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Avoiding for loop

From: James Tursa

Date: 23 Jul, 2010 20:40:26

Message: 10 of 14

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2csjm$b49$1@fred.mathworks.com>...
> You are both right, compute expm(B) outside the loop first. However, I need all the values of V. -a<= x1,x2,x3,x4 <=a, where a can be any integer.

Why? Do you need all of them at the same time? What are you doing with V in your code?

James Tursa

Subject: Avoiding for loop

From: Matt J

Date: 23 Jul, 2010 20:42:05

Message: 11 of 14

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2csjm$b49$1@fred.mathworks.com>...
> You are both right, compute expm(B) outside the loop first. However, I need all the values of V. -a<= x1,x2,x3,x4 <=a, where a can be any integer.
=============

I'm willing to lay odds that you don't need this and that it would be advisable for you to explain the bigger picture of what you're trying to do.

Just for fun, though, the for-loops can be eliminated from Andy's solution as follows:

[x1,x2]=ndgrid(-10:0.1:10, -10:0.1:10);

XX=[x1(:).';x2(:).'];

tmp1 = sum( XX.*(A*XX) );
tmp2 = XX'*expm(B);

V=bsxfun(@plus, tmp1, tmp2*XX);

Subject: Avoiding for loop

From: Stefan

Date: 25 Jul, 2010 11:19:04

Message: 12 of 14

Thx for the solution without the for loops.
The bigger picture is that there is a second function V2, with matrices A2 & B2.
I'm trying to find all the points (x1,x2,x3,x4) where V<V2, and x1,x2,x3,x4 are changing by a 0.1 stepsize.

> I'm willing to lay odds that you don't need this and that it would be advisable for you to explain the bigger picture of what you're trying to do.
>
> Just for fun, though, the for-loops can be eliminated from Andy's solution as follows:
>
> [x1,x2]=ndgrid(-10:0.1:10, -10:0.1:10);
>
> XX=[x1(:).';x2(:).'];
>
> tmp1 = sum( XX.*(A*XX) );
> tmp2 = XX'*expm(B);
>
> V=bsxfun(@plus, tmp1, tmp2*XX);

Subject: Avoiding for loop

From: Matt J

Date: 25 Jul, 2010 14:44:38

Message: 13 of 14

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2h6j8$659$1@fred.mathworks.com>...
> Thx for the solution without the for loops.
> The bigger picture is that there is a second function V2, with matrices A2 & B2.
> I'm trying to find all the points (x1,x2,x3,x4) where V<V2, and x1,x2,x3,x4 are changing by a 0.1 stepsize.
=======

That's not really the big picture. The big picture would explain why you think you need all values for all 201^4 combinations of (x1,x2,x3,x4) in memory simultaneously.

Subject: Avoiding for loop

From: mecej4

Date: 10 Oct, 2010 18:13:52

Message: 14 of 14

On 7/23/2010 2:10 PM, Stefan wrote:
>
> for x1 = -10:0.1:10;
> for x2 = -10:0.1:10;
> for x3 = -10:0.1:10;
> for x4 = -10:0.1:10;
>
> V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]
> end
> end
> end
> end
Since each assignment to V other than the first overwrites the previous
one, you can replace the above code by

     x1 = 10; x2 = 10; x3 = 10; x4 = 10;
     V =[x1;x2]'*A*[x1;x2] + [x1;x2]'*expm(B)*[x3;x4]

-- mecej4

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