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:
How can I make the following loop more efficient?

Subject: How can I make the following loop more efficient?

From: Tanya Rosenblit

Date: 16 Dec, 2010 15:57:21

Message: 1 of 4

Hello,

How can I make the following loop more efficient?

for r=0:r1-2,
    for c = 0:c1-2,
        dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
        dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
    end;
end;

c1 and r1 are constant.

Thank you!!!
Tanya
 

Subject: How can I make the following loop more efficient?

From: Paulo Silva

Date: 16 Dec, 2010 16:32:05

Message: 2 of 4

"Tanya Rosenblit" <tanyaros83@gmail.com> wrote in message <iedct1$i0d$1@fred.mathworks.com>...
> Hello,
>
> How can I make the following loop more efficient?
>
> for r=0:r1-2,
> for c = 0:c1-2,
> dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
> dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
> end;
> end;
>
> c1 and r1 are constant.
>
> Thank you!!!
> Tanya
>

Preallocate dx_max, dx_min, dy_max, dy_min.

Subject: How can I make the following loop more efficient?

From: Sean de

Date: 16 Dec, 2010 16:53:05

Message: 3 of 4

"Tanya Rosenblit" <tanyaros83@gmail.com> wrote in message <iedct1$i0d$1@fred.mathworks.com>...
> Hello,
>
> How can I make the following loop more efficient?
>
> for r=0:r1-2,
> for c = 0:c1-2,
> dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
> dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
> end;
> end;
>
> c1 and r1 are constant.
>
> Thank you!!!
> Tanya
>

%% Data

r1 = 12;
c1 = 22;

%% Your way
for r=0:r1-2,
    for c = 0:c1-2,
        dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
        dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
    end;
end;


%% meshgrid way
[dx_max2 dy_max2] = meshgrid(c1-2:-1:0,r1-2:-1:0);
[dx_min2 dy_min2] = meshgrid([0 0:-1:-(c1-2)],[0 0:-1:-(r1-2)]);

%% Insure Equality

isequal(dx_max2,dx_max)
isequal(dy_max2,dy_max)
isequal(dx_min2,dx_min)
isequal(dy_min2,dy_min)

%SCd

Subject: How can I make the following loop more efficient?

From: Daphne

Date: 19 Dec, 2010 14:46:04

Message: 4 of 4


Thanks for the suggestion!
The meshgrid idea looks interesting. However, even in your example, the isequal returns a false - they are not equal.
I think that the meshgrid just takes care of the loop, but doesn't input the values into where they should be?

We would really appreciate any further suggestions :-)
Thanks,
Daphne


"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iedg5h$c8$1@fred.mathworks.com>...
> "Tanya Rosenblit" <tanyaros83@gmail.com> wrote in message <iedct1$i0d$1@fred.mathworks.com>...
> > Hello,
> >
> > How can I make the following loop more efficient?
> >
> > for r=0:r1-2,
> > for c = 0:c1-2,
> > dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
> > dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
> > end;
> > end;
> >
> > c1 and r1 are constant.
> >
> > Thank you!!!
> > Tanya
> >
>
> %% Data
>
> r1 = 12;
> c1 = 22;
>
> %% Your way
> for r=0:r1-2,
> for c = 0:c1-2,
> dx_max(r+1,c+1) = c1-c-2; dx_min(r+2,c+2) = -c;
> dy_max(r+1,c+1) = r1-r-2; dy_min(r+2,c+2) = -r;
> end;
> end;
>
>
> %% meshgrid way
> [dx_max2 dy_max2] = meshgrid(c1-2:-1:0,r1-2:-1:0);
> [dx_min2 dy_min2] = meshgrid([0 0:-1:-(c1-2)],[0 0:-1:-(r1-2)]);
>
> %% Insure Equality
>
> isequal(dx_max2,dx_max)
> isequal(dy_max2,dy_max)
> isequal(dx_min2,dx_min)
> isequal(dy_min2,dy_min)
>
> %SCd

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