3

I have a for loop nested thrice in a matlab program. Can any of you help me optimize it.

w=5;
a = rand(m*n,10); b=rand(m,n);
for i = 1 : m
   for j = 1 : n
      for k = 1 : l
         if (i-w >= 1 && i+w <= m)
             featureL = a(((i-1)*n)+j,:); featureR = a(((i-1)*n)+j-d,:);
             D1(i,j,k) = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
             D2(i,j,k) = mean2( b(i-w:i+w, j-w:j+w) );
         end
      end
   end
end

I know the performance could be heavily improved by using meshgrid, but I am not sure how to do it.

Thanks in anticipation.

Can it be done something like this..

[X Y Z] = meshgrid(1:m,1:n,1:l);
D1(something containing X,Y,Z) = sqrt( sum( ( a(something cont. X,Y) - a(something cont. X,Y)).*(a(something cont. X,Y) - a(something cont. X,Y)) ) );
% similarly D2

Thanks a lot!.

5
  • 2
    Not a full answer but a quick hint to begin: avoid the if-statement by modifying the most-outter loop like this: for i=1+w:m-w. Commented Jul 19, 2012 at 11:44
  • 1
    Can you specify the values of m, n, l and d that you're working with? Commented Jul 19, 2012 at 12:30
  • @Bentoy13 this is certainly a first improvement. Also i don't see any influence of k on the calculations. As far as I can see all pages are the same -> use repmat. Commented Jul 19, 2012 at 12:49
  • 1
    Always good for optimization - the output of mlint your_file_name It gives lots of warnings you should preallocate D1 and D2. Commented Jul 19, 2012 at 13:17
  • @raghudeep: I suspect you have a typo: what is the value of d? is it the same as w? also how are the formulas depending on k? Please double check your code.. Commented Jul 20, 2012 at 23:07

1 Answer 1

2

I've found that a good way to attack these things is incrementally. Start by examining everything in the innermost loop, and see if it can be done at a higher level. This will reduce repeated computations.

For example, you can perform your if (i-w >= 1 && i+w <= m) two levels higher (since it only depends on i,w, and m), reducing if checks and skipping loop iterations. Once that is done, your featureL and featureR calculations can be moved up one level; they are performed inside the k loop but only depend on j. Similarly, sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) ) can be computed outside of the k loop, put into a variable, and assigned later.

In fact, as far as I can see you can get rid of the entire k loop since k is never used. Here's your code with some of this applied:

w=5; 
a = rand(m*n,10); 
b=rand(m,n); 
for i = 1 : m
    if (i-w >= 1 && i+w <= m)
        for j = 1 : n
            featureL = a(((i-1)*n)+j,:); 
            featureR = a(((i-1)*n)+j-d,:);
            x = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
            y = mean2( b(i-w:i+w, j-w:j+w) )           
            D1(i,j,:) = x;              
            D2(i,j,:) = y;
        end    
    end 
end
Sign up to request clarification or add additional context in comments.

1 Comment

Only one more thing, in this code you are iterating from 1 to m, but the if condition does that the real valid values were from i+w (6 in your code) to m-w. Then you can redefine the for i range and delete the if condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.