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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How can I reduce the time complexity of this matlab code.. Its taking lot of time to execute when an image is fed as input.

Asked by .. Vamshi on 23 Nov 2012
    cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;
    p = 1;
    row = 1;
    while p<(size(Theta_deg_img,1))
        q = 1;   
        while q<(size(Theta_deg_img,2))
            for i=p:p+8             
                for j=q:q+8                                                 
                    if Theta_deg_img(i,j)>=-180 && Theta_deg_img(i,j)<=-135                    
                        cnt1 = cnt1+1;                
                    elseif Theta_deg_img(i,j)>=-134 && Theta_deg_img(i,j)<=-90                   
                        cnt2 = cnt2 + 1;                                    
                    elseif Theta_deg_img(i,j)>=-89 && Theta_deg_img(i,j)<=-45                    
                        cnt3 = cnt3 + 1;                
                    elseif Theta_deg_img(i,j)>=-44 && Theta_deg_img(i,j)<=0                    
                        cnt4 = cnt4 + 1;                
                    elseif Theta_deg_img(i,j)>=1 && Theta_deg_img(i,j)<=45                    
                        cnt5 = cnt5 + 1;                
                    elseif Theta_deg_img(i,j)>=46 && Theta_deg_img(i,j)<=90                   
                        cnt6 = cnt6 + 1;                
                    elseif Theta_deg_img(i,j)>=91 && Theta_deg_img(i,j)<=135                 
                        cnt7 = cnt7 + 1;                                
                    else                        
                        cnt8 = cnt8 + 1;
                    end                    
                    eval([fsrc '(row,1) = cnt1;']);
                    eval([fsrc '(row,2) = cnt2;']);
                    eval([fsrc '(row,3) = cnt3;']);
                    eval([fsrc '(row,4) = cnt4;']);
                    eval([fsrc '(row,5) = cnt5;']);
                    eval([fsrc '(row,6) = cnt6;']);
                    eval([fsrc '(row,7) = cnt7;']);
                    eval([fsrc '(row,8) = cnt8;']);                                     
                end                               
            end           
            q = q + 9;  row = row+1;        
            cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;                    
        end         
        p = p + 9;        
    end        

0 Comments

.. Vamshi

Products

No products are associated with this question.

2 Answers

Answer by Harshit on 23 Nov 2012
Accepted answer

Vectorize your code. Example you don't need inner loops you can use sum to get the value of counts. In the outer two loops you are calculating size again and again. Store it in a variable.

5 Comments

.. Vamshi on 26 Nov 2012

mmm... I want here to increment the count every time when the condition is met. Does the above piece of code satisfy my requirement?

Harshit on 26 Nov 2012

Yes it will just think about it. Whenver the condn is true 1 will be generated in the matrix and otherwise 0. sum will give the no of 1.

.. Vamshi on 26 Nov 2012

I followed your suggestion.. the counts have to be stored for every iteration.. but here am unable to do..

Harshit
Answer by Akiva Gordon on 23 Nov 2012

1 Comment

.. Vamshi on 23 Nov 2012

Thank you A G. I think eval is fine here for concatenations, isn't it?

Akiva Gordon

Contact us