[Scilab-users] Running average

CRETE Denis denis.crete at thalesgroup.com
Tue Feb 12 00:08:28 CET 2013


Hello, 
I looked for a function to make a "running average", i.e. an average of a sub vector v:
sv=10000;v=rand(sv,1); km=5; // Running average over 2*km+1 = 11 points
for i=1:size(v,'*'), 
	suite=i-min(i-1,km):i+min(size(v,'*')-i,km); 
	vra_1(i)=sum(v(suite))/size(suite,'*');
end;
Is there a function doing already this in Scilab, or is there a more efficient (compact/rapid) way to do this ?
I tried with sparse matrix (with a "broad diagonal"), but it is much longer:
// Build sparse matrix
spM=[];
for i=1:sv, 
	for j=-km:km; 
		if (i+j>0 & i+j-1<sv), spM=[spM;i,i+j,1]; end;
	end;
	im=find(spM(:,1)==i);
	spM(im,3)=1./size(im,'*');
end;
// Calculate running average
vra_2=sparse(spM(:,1:2),spM(:,3))*v;

Thank you in advance for any improvement.
Best regards
Denis



More information about the users mailing list