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:
Help with for loops

Subject: Help with for loops

From: mohamad ali

Date: 8 Apr, 2011 04:13:04

Message: 1 of 2

Hello there,

I'd like some help in optimizing the script below.On large scale it's taking a lot of time.

Feasible_Starting_Flow=zeros(size(segments,1),1);
for i=1:size(SR_MinLength_Path,1)
        for j=3:size(SR_MinLength_Path,2)-1
            b1=SR_MinLength_Path(i,j);
            b2=SR_MinLength_Path(i,j+1);
            for x=1:size(segments,1)
                if (segments (x,2)==b1 && segments (x,3)==b2)
                 Feasible_Starting_Flow(x,1)=Feasible_Starting_Flow(x,1)+Traffic_Generated(SR_MinLength_Path(i,1),SR_MinLength_Path(i,2));
                end
            end

       
        end
    end

Ex: if the first row of the SR_Min_Length_Path entry was like :1 2 1 6 7 3 8 2 0 0 0.34.My script would start by taking the third and fourth entries and put them in b1 and b2 respectively.At the beginning b1=1 and b2=6.The script then checks if the 2 entries are found in the second and third columns of the segments matrix.If so, it will take the value of the Traffic_Generated(SR_MinLength_Path(i,1),SR_MinLength_Path(i,2)) which are basically the Traffic_Generated(1,2) "the first 2 entries in the row and put it in the Feasible_Starting_Flow matrix."It will keep updating the b1 and b2 until all the entries in the row are over and then it moves to the second row and so the same thing until all rows are done.

Mainly what am asking is I want to get rid of the for loops if possible so that processing time get shorter.

Any help is greatly appreciated.

Subject: Help with for loops

From: Roger Stafford

Date: 10 Apr, 2011 00:50:18

Message: 2 of 2

"mohamad ali " <itani.mai@hotmail.com> wrote in message <inm20g$40f$1@fred.mathworks.com>...
> Hello there,
>
> I'd like some help in optimizing the script below.On large scale it's taking a lot of time.
>
> Feasible_Starting_Flow=zeros(size(segments,1),1);
> for i=1:size(SR_MinLength_Path,1)
> for j=3:size(SR_MinLength_Path,2)-1
> b1=SR_MinLength_Path(i,j);
> b2=SR_MinLength_Path(i,j+1);
> for x=1:size(segments,1)
> if (segments (x,2)==b1 && segments (x,3)==b2)
> Feasible_Starting_Flow(x,1)=Feasible_Starting_Flow(x,1)+Traffic_Generated(SR_MinLength_Path(i,1),SR_MinLength_Path(i,2));
> end
> end
> end
> end
> ........
- - - - - - - - -
  I have answered your earlier copy of this request in:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305773

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