Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help speeding up/replacing loops
Date: Tue, 16 Jun 2009 12:56:54 -0400
Organization: The MathWorks, Inc.
Lines: 44
Message-ID: <h18irl$1um$1@fred.mathworks.com>
References: <h18ge7$gta$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1245171381 2006 144.212.105.187 (16 Jun 2009 16:56:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 16 Jun 2009 16:56:21 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:548006



"James Wright" <jameswright1001@yahoo.co.uk> wrote in message 
news:h18ge7$gta$1@fred.mathworks.com...
> I've written some code, however it takes forever (several days) to run for 
> larger values. Can anyone with more experience in matlab help me with the 
> issue?? I understand vectorization is the way forward, but I'm unaware of 
> how to do this for most of my loops.
> Many thanks
>
> Current Code:
>
> function [] = the021testgraphnew(a1,an,aacc,x,N1,N2,g)
> N = N1-N2;
> ncut = N/10;
> b = 0;
> count = 0;
> 'creates the vector (1,2,...,ncut)';
> X = 1:1:ncut;
> 'creates a vector with g random values';
> C(1:g) = (rand(1)*pi);
> 'loops everything for each value of a (aka mu), this is time consuming but 
> I cannot see another way around it';
> for a = a1:aacc:an
>    b = b + 1;
>    A(b) = a;

MLINT should be flagging this line, and many of the other lines where you 
create matrices inside of loops, and telling you to preallocate.  Try 
preallocating your matrices outside the loops and see if that improves the 
performance.

MLINT:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-11863.html

Preallocation:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html

*snip*

-- 
Steve Lord
slord@mathworks.com