0
\$\begingroup\$

I have run this program for five hours and it has not yet completed. I wonder if we can make changes to reduce the computation time.

I use several for loop and this greatly increases the computation time. my goal is to compare the contents of the rows of a matrix (4435 × 2000) with 6 row vectors (1 × 2000). The comparison is made on parts of 64 values.

for i=1:4435          
    for j=70:2000      
        L = A(i,j);      
        j1=0;     
        j2=0;     
        j3=0;     
        j4=0;     
        j5=0;     
        j6=0;      
        for i1=70:2000  
            if D1(B(v,1),i1)==L  
                j1=i1;  
                Break  
            end  
        end

        for i2=1:2000
            if A(B(v-1,1),i2)==L
                j2=i2;
                Break
            end
        end

        for i3=1:2000
            if A(B(v-2,1),i3)==L
                j3=i3;
                Break
            end
        end

        for i4=1:2000
            if A(C(mm,1),i4)==L
                j4=i4;
                Break
            end
        end

        for i5=1:2000
            if A(C(mm-1,1),i5)==L
                j5=i5;
                Break
            end
        end

        for i6=1:2000
            if A(C(mm-2,1),i6)==L
                j6=i6;
                Break
            end
        end

        if j1>64             
           if A(i,j-63:j)==A(B(v,1),j1-63:j1)
              m_A1(i,j)=1;
           else
              m_A1(i,j)=0;
           end
        end 

        if j2>64             
           if A(i,j-63:j)==A(B(v-1,1),j2-63:j2)
              m_A2(i,j)=1;
           else
              m_A2(i,j)=0;
           end
        end 

        if j3>64             
           if A(i,j-63:j)==A(B(v-2,1),j3-63:j3)
              m_A3(i,j)=1;
           else
              m_A3(i,j)=0;
           end
        end 

        if j4>64             
           if A(i,j-63:j)==A(C(mm,1),j4-63:j4)
              m_A4(i,j)=1;
           else
              m_A4(i,j)=0;
           end
        end 

         if j5>64             
           if A(i,j-63:j)==A(C(mm-1,1),j5-63:j5)
              m_A5(i,j)=1;
           else
              m_A5(i,j)=0;
           end
         end 

         if j6>64             
           if A(i,j-63:j)==A(C(mm-2,1),j6-63:j6)
              m_A6(i,j)=1;
           else
              m_A6(i,j)=0;
           end
        end 

    end
end
\$\endgroup\$

migrated from stackoverflow.com Dec 10 '11 at 20:13

This question came from our site for professional and enthusiast programmers.

3
\$\begingroup\$

In Matlab, unlike C, you have to vectorize your code. A first step would be to understand how to use find like this:

i1=70:2000;
j1=find(D1(B(v,1),i1)==L);

and:

if j1>64             
       m_A1(i,j) = all( A(i,j-63:j)==A(B(v,1),j1-63:j1) );
end 

You may also find any and all useful. Eventually you should be able to eliminate all the for loops and if statements.

\$\endgroup\$
  • \$\begingroup\$ thank you for your answer, but could you, please, also change the part of the program: if j1>64 if A(i,j-63:j)==A(B(v,1),j1-63:j1) m_A1(i,j)=1; else m_A1(i,j)=0; end end \$\endgroup\$ – user319336 Dec 10 '11 at 19:02
  • \$\begingroup\$ Here you go. See the update. \$\endgroup\$ – cyborg Dec 10 '11 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.