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:
optimize my script

Subject: optimize my script

From: mohamad ali

Date: 6 Apr, 2011 16:58:04

Message: 1 of 3

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: optimize my script

From: Roger Stafford

Date: 10 Apr, 2011 00:43:04

Message: 2 of 3

"mohamad ali " <itani.mai@hotmail.com> wrote in message <ini62s$jdc$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
> ........
- - - - - - - - - - - -
  Your task may be performed faster if you first sort the 'segments' 2nd and 3rd columns to lexicographical (dictionary) order so as to enable faster searching through it for pairs in the rows of 'SR_MinLength_Path'. The following is one example of that. I have taken the liberty of renaming four of your arrays as:

 s <-- segments
 P <-- SR_MinLength_Path
 T <-- Traffic_Generated
 F <-- Feasible_Starting_Flow

  After placing the unique, sorted pairs from s(:,2:3) into u, the pairs of each row of P(:,3:end) are subjected to a binary search in u to find possible matches. Assuming u is reasonably large this should be faster with a binary search than doing a serial search, as in your current script. In the last step the counts in F are rearranged (and possible duplications in s expanded) into the original order in s.

 [u,~,ux] = unique(s(:,2:3),'rows'); % Sort unique pairs in s
 n = size(u,1);
 F = zeros(n,1);
 m = size(P,2);
 for i = 1:size(P,1); % Do this once for each row of P
   a = ones(m-3,1); % Initial lower bound pointer
   b = (n+1)*a; % Initial upper bound
   c = floor((a+b)/2); % Initial point of testing
   g = P(i,3:m-1).'; % g and h are pairs from current row of P
   h = P(i,4:m).';
   for k = 1:ceil(log2(n)) % Repeat until c can no longer change
     t = g<u(c,1) | (g==u(c,1) & h<u(c,2)); % g,h pair < u(c,:) pair ?
     a = t.*(a-c)+c; % If false, raise the lower bound to c
     b = t.*(c-b)+b; % If true, lower the upper bound to c
     c = floor((a+b)/2); % Set c to new midpoint
   end
   f = c(g==u(c,1) & h==u(c,2)); % Is there an actual match?
   F = F + accumarray(f,T(P(i,1),P(i,2)),size(u,1)); % Update F from T
 end
 F = F(ux); % Transform F to correspond to original s order

Roger Stafford

Subject: optimize my script

From: mohamad ali

Date: 10 Apr, 2011 22:13:05

Message: 3 of 3

Thanks Roger,

I've tried out the code and it gave the same results.I haven't tested it on large scale problems yet but am sure it's gonna take less time to compute than my script.Anyhow,

one small modification "F = F + accumarray(f,T(P(i,1),P(i,2)),[n 1]); % Update F from T" is that the size has to be vector not a single entry.

Cheers mate...

Mohamad Ali

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