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 nested loops

Subject: vectorizing nested loops

From: Peter

Date: 7 Dec, 2010 20:26:06

Message: 1 of 3

I'm looking for a simple vectorization of a nested loop and can't quite get it working properly. A simple example appears below:
n1=5
n2=10
n3=15
count=0
mindin=2
        for w1=(n1:1:n2)
     for w2=w1+mindin:1:n2+mindin
         for w3=w2+mindin:1:n3+mindin
             count=count+1;
            bigbreaks(count,1:3)=horzcat(w1,w2,w3);
         end
     end
 end
count
bigbreaks

Is there an easy way to vectorize the construction of the bigbreaks matrix? Would appreciate a gentle nudge !

Subject: vectorizing nested loops

From: Sean de

Date: 7 Dec, 2010 20:54:21

Message: 2 of 3

"Peter " <psephton@business.queensu.ca> wrote in message <idm58u$1gc$1@fred.mathworks.com>...
> I'm looking for a simple vectorization of a nested loop and can't quite get it working properly. A simple example appears below:
> n1=5
> n2=10
> n3=15
> count=0
> mindin=2
> for w1=(n1:1:n2)
> for w2=w1+mindin:1:n2+mindin
> for w3=w2+mindin:1:n3+mindin
> count=count+1;
> bigbreaks(count,1:3)=horzcat(w1,w2,w3);
> end
> end
> end
> count
> bigbreaks
>
> Is there an easy way to vectorize the construction of the bigbreaks matrix? Would appreciate a gentle nudge !

One way:
[i1 i2 i3] = ndgrid(n1:n2,(n1+mindin):(n2+mindin),(n1+mindin):(n3+mindin));
bigbreaks2 = [i1(:) i2(:) i3(:)];

idx2remove = (i1(:)>(i2(:)-mindin))|(i1(:)>(i3(:)-2*mindin))|(i2(:)>(i3(:)-mindin));
bigbreaks2(idx2remove,:) = [];
bigbreaks2 = sortrows(bigbreaks2,[1 2 3]);
isequal(bigbreaks,bigbreaks2)

Subject: vectorizing nested loops

From: Peter

Date: 7 Dec, 2010 21:09:05

Message: 3 of 3

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idm6tt$khk$1@fred.mathworks.com>...
> "Peter " <psephton@business.queensu.ca> wrote in message <idm58u$1gc$1@fred.mathworks.com>...
> > I'm looking for a simple vectorization of a nested loop and can't quite get it working properly. A simple example appears below:
> > n1=5
> > n2=10
> > n3=15
> > count=0
> > mindin=2
> > for w1=(n1:1:n2)
> > for w2=w1+mindin:1:n2+mindin
> > for w3=w2+mindin:1:n3+mindin
> > count=count+1;
> > bigbreaks(count,1:3)=horzcat(w1,w2,w3);
> > end
> > end
> > end
> > count
> > bigbreaks
> >
> > Is there an easy way to vectorize the construction of the bigbreaks matrix? Would appreciate a gentle nudge !
>
> One way:
> [i1 i2 i3] = ndgrid(n1:n2,(n1+mindin):(n2+mindin),(n1+mindin):(n3+mindin));
> bigbreaks2 = [i1(:) i2(:) i3(:)];
>
> idx2remove = (i1(:)>(i2(:)-mindin))|(i1(:)>(i3(:)-2*mindin))|(i2(:)>(i3(:)-mindin));
> bigbreaks2(idx2remove,:) = [];
> bigbreaks2 = sortrows(bigbreaks2,[1 2 3]);
> isequal(bigbreaks,bigbreaks2)

Thanks very much.

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