[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: address@hidden: address@hidden: Re:]]
> N=100;n=3;a=rand(N,1);
> result=shift(conv(a,ones(1,n)),-fix(n/2))(1:N)/n;
> result=filter([ones(1,n),zeros(1,N-n)],eye(1,N),a)/n;
Just as an example of a different way of writing the same thing, what I
did was:
N=10000; n=10; a=rand(N,1);
tic;b=zeros(N-n+1,1);for i=1:N-n+1 b(i)=mean(a(i:i+n-1));endfor;toc
tic;c=(cumsum(prepad(a,N+1)));c=(c(n+1:end)-c(1:end-n))/n;toc,max(abs(b-c))
tic;d=filter(ones(n,1),n,a)(n:end);toc,max(abs(b-c))
The only difference from yours is that I remove the edges, that is, I
get a moving average vector that is N-n+1 elements long.
The one using cumsum takes about 1.5 times longer than the one using
filter, and this speed ratio is independent of N (tried for N=1e4, 1e5,
1e6, 1e7).
--
Francesco Potortì (ricercatore) Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR Fax: +39 050 313 8091
via G. Moruzzi 1, I-56124 Pisa Email: address@hidden
Web: http://fly.isti.cnr.it/ Key: fly.isti.cnr.it/public.key
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------