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:
vectorizing loops

Subject: vectorizing loops

From: Gregory Carnegie

Date: 2 Nov, 2010 16:52:03

Message: 1 of 4

hello people,
i'm sort of a novice matlab user at best. however, i have been tasked with optimizing a peice of code:

for k=1:3
    for a=1:N
        disp(['N = ',num2str(a),' done ',num2str(k)])
        for b=1:N
            if k==1
                s(a,b)=0.;
            end
            off(k)=0;
            if (pos(k,a)<=0.25)
                if (pos(k,b)>=0.75)
                    off(k)=1;
                end
            end
            if (pos(k,a)>=0.75)
                if (pos(k,b)<=0.25)
                    off(k)=-1
                end
            end
            s(a,b)=s(a,b)+(pos(k,a)-pos(k,b)+off(k))^2.0;
            if k==3
                s(a,b)=sqrt(s(a,b));
            end
        end
    end
end

match=zeros(1,N);

for a=1:N
    mindist=1e10;
    for c=1:N
        if (a~=c)
            mindist=min(s(a,c),mindist);
            if (mindist==s(a,c))
                match(a)=c;
            end
        end
    end
end

does anybody know of any books, videos or any sort of media that can help me learn how to do this.
p.s. i know about preallocation of variables. i am more interested in vectorizing loops.

Subject: vectorizing loops

From: Oleg Komarov

Date: 2 Nov, 2010 17:00:07

Message: 2 of 4

Give a look to the "Code Vectorization Guide":
http://www.mathworks.com/support/tech-notes/1100/1109.html

Oleg

Subject: vectorizing loops

From: Roger Stafford

Date: 3 Nov, 2010 00:58:03

Message: 3 of 4

"Gregory Carnegie" <gregorick@hotmail.com> wrote in message <iapfjj$6s2$1@fred.mathworks.com>...
> hello people,
> i'm sort of a novice matlab user at best. however, i have been tasked with optimizing a peice of code:
>
> for k=1:3
> for a=1:N
> disp(['N = ',num2str(a),' done ',num2str(k)])
> for b=1:N
> if k==1
> s(a,b)=0.;
> end
> off(k)=0;
> if (pos(k,a)<=0.25)
> if (pos(k,b)>=0.75)
> off(k)=1;
> end
> end
> if (pos(k,a)>=0.75)
> if (pos(k,b)<=0.25)
> off(k)=-1
> end
> end
> s(a,b)=s(a,b)+(pos(k,a)-pos(k,b)+off(k))^2.0;
> if k==3
> s(a,b)=sqrt(s(a,b));
> end
> end
> end
> end
>
> match=zeros(1,N);
>
> for a=1:N
> mindist=1e10;
> for c=1:N
> if (a~=c)
> mindist=min(s(a,c),mindist);
> if (mindist==s(a,c))
> match(a)=c;
> end
> end
> end
> end
>
> does anybody know of any books, videos or any sort of media that can help me learn how to do this.
> p.s. i know about preallocation of variables. i am more interested in vectorizing loops.
- - - - - - - - - - - -
  That entire first set of three nested for-loops can be replaced with the following:

 [A,B] = ndgrid(1:N,1:N);
 pA = pos(:,A);
 pB = pos(:,B);
 s = reshape(...
        sqrt(sum((pA-pB+(pA<=.25&pB>=.75)-(pA>=.75&pB<=.25)).^2)) ...
       ,N,N);

A similar reduction can replace the second set of for-loops but I will let you discover it for yourself. Hint: read the documentation for 'min' carefully.

Roger Stafford

Subject: vectorizing loops

From: Gregory Carnegie

Date: 5 Nov, 2010 18:58:05

Message: 4 of 4

thanks so much, that was a great help

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