Path: news.mathworks.com!not-for-mail
From: "Sean Douglas" <seanjdouglas@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: moving average code taking too long
Date: Tue, 1 Jun 2010 20:48:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 42
Message-ID: <hu3rm4$jp7$1@fred.mathworks.com>
References: <hu3la9$4hp$1@fred.mathworks.com> <hu3nbc$o7m$1@fred.mathworks.com>
Reply-To: "Sean Douglas" <seanjdouglas@hotmail.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1275425284 20263 172.30.248.35 (1 Jun 2010 20:48:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 1 Jun 2010 20:48:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1894011
Xref: news.mathworks.com comp.soft-sys.matlab:641283

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hu3nbc$o7m$1@fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <hu3la9$4hp$1@fred.mathworks.com>...
> > I just wrote a moving average program, but it is taking about 10 minutes to run and this is too long.   Here is the code I am using on a small example.
> > 
> >        a=       20041126                      7.11          
> >                   20041127                      2.33
> >                   20041128                      5.88
> >                   20041129                      4.67
> >                   20041130                      4.66
> >                   20041201                      3.01
> >                   20041203                      2.58
> >                   20041204                      1.35
> > 
> > 
> > MA=3
> > n=length(a)
> > k=n - MA
> > m=1
> > 
> >     for q=1:n+1 - MA
> >     j=m:MA+m - 1
> >     Mavg(q)=sum(a(j,2))/MA
> >     m=m+1
> >     end
> > 
> > so i think this for statement is taking way way too long, expecially when using 3000 columns of data and figuring a Moving Average of 150 rather then the MA=3 I am using in this example to show you guys what I am up to.
> > 
> 
> I would point out that the correct way to do this is with
> conv or filter. These solutions are far better choices than
> the cumsum trick, which will have precision issues.
> 
> However, nobody has pointed out why your code is so
> miserably slow. Learn to preallocate that array! It is
> because you have not preallocated it to the final size
> that your loop is slow.
> 
> John

thank you John and everyone else I am looking up  "conv "and "preallocate array"    so I can get this program working faster.

thanks