From: Sean Douglas on 1 Jun 2010 14:59 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. thank you sean
From: Walter Roberson on 1 Jun 2010 15:14 Sean Douglas wrote: > 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 If I understand correctly, the fastest way to do that is to use conv(). blkproc() could also be used, but that would likely be slower than conv(), and there have been reports that blockproc(), the replacement for blkproc(), is sometimes considerably slower than blkproc(). Another approach is to use cumsum, as you can then calculate all the averages over the range in one go by using (TheCumSum(MA+1:end) - TheCumSum(1:end-MA)) ./ MA Note this is limited by the precision of the cumulative summation rather than by the precision of each segment.
From: Roger Stafford on 1 Jun 2010 15:16 "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <hu3la9$4hp$1(a)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. > > thank you sean Do a cumsum on the numbers and then take differences among these MA apart. That can be done without a for-loop. It also cuts down on the number of additions required. Roger Stafford
From: us on 1 Jun 2010 15:19 "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <hu3la9$4hp$1(a)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. > > thank you sean a hint: help filter; us
From: John D'Errico on 1 Jun 2010 15:34 "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <hu3la9$4hp$1(a)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
|
Next
|
Last
Pages: 1 2 3 Prev: Proper GRNN use in Matlab Next: Filter design/implementation question |