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:
loops and speed

Subject: loops and speed

From: muzaffar

Date: 4 Aug, 2008 12:38:01

Message: 1 of 5

Hi all,
I have number of nested for loops and it takes long time
to process.
any suggestion to reduce processing time?
regards.

Subject: loops and speed

From: heiko_marx@hotmail.com

Date: 4 Aug, 2008 13:03:08

Message: 2 of 5

On 4 Aug., 14:38, "muzaffar " <muzaffarbas...@yahoo.com> wrote:
> Hi all,
> I have number of nested for loops and it takes =A0long time
> to process.
> any suggestion to reduce processing time?
> regards.

Hi.

Well, yes. No. It depends.

Next time, please try to be more specific :-)! Code examples help.

Heiko

Subject: loops and speed

From: Steve Amphlett

Date: 4 Aug, 2008 14:49:01

Message: 3 of 5

"muzaffar " <muzaffarbashir@yahoo.com> wrote in message
<g76t79$oge$1@fred.mathworks.com>...
> Hi all,
> I have number of nested for loops and it takes long time
> to process.
> any suggestion to reduce processing time?
> regards.

Without seeing your code the only sensible suggestions
anyone can make are:

1) Use the profiler to find your bottleneck(s).
2) Preallocate any matrices that are grown in the loop.

Subject: loops and speed

From: Arjav Chakravarti

Date: 4 Aug, 2008 17:10:19

Message: 4 of 5

You can use parallel for loops:
http://www.mathworks.com/products/parallel-computing/description2.html


"muzaffar " <muzaffarbashir@yahoo.com> wrote in message
news:g76t79$oge$1@fred.mathworks.com...
> Hi all,
> I have number of nested for loops and it takes long time
> to process.
> any suggestion to reduce processing time?
> regards.
>

Subject: loops and speed

From: muzaffar

Date: 9 Oct, 2008 13:53:22

Message: 5 of 5

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <g774st$s5g$1@fred.mathworks.com>...
> "muzaffar " <muzaffarbashir@yahoo.com> wrote in message
> <g76t79$oge$1@fred.mathworks.com>...
> > Hi all,
> > I have number of nested for loops and it takes long time
> > to process.
> > any suggestion to reduce processing time?
> > regards.
>
> Without seeing your code the only sensible suggestions
> anyone can make are:
>
> 1) Use the profiler to find your bottleneck(s).
> 2) Preallocate any matrices that are grown in the loop.

Here is some code example: is it possible to make it more fast?
t and r are two linear arrays.
N, and M are two numbers.

d=(repmat(r',1,N)-repmat(t,M,1)).^2;

D=zeros(size(d));
D(1,1)=2*d(1,1);

for m=2:M
    D(m,1)=d(m,1)+D(m-1,1);
end
for n=2:N
    D(1,n)=d(1,n)+D(1,n-1);
end

D(2,2) = D(1,1)+2*d(2,2);


for m=3:M
    D(m,2)= min(D(m-1,1)+d(m,2),D(m-2,1)+2*d(m-1,2))+d(m,2);
end
for n=3:N
    D(2,n)= min(D(1,n-1)+d(2,n),D(1,n-2)+2*d(2,n-1))+d(2,n);
end

kind regards.

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