[Scilab-users] efficient calculation for a moving window

Rafael Guerra jrafaelbguerra at hotmail.com
Sat Mar 25 16:44:16 CET 2017


You can try vectorization for speed at cost of memory - see code below.
It should be pretty general as long as f can work on columns.
Note that for the moving average example I have used, a convolution
implementation would be much faster.

// START OF CODE
n=100;
m = 20;

function y=f(x)
// function f can be anything but must operate on columns of input x
    y = mean(x,'c');  
endfunction

// Initial loop solution:
y = rand(1,n);
y1 = zeros(1,n-m+1);
for i=1:n-m+1
    y1(i)=f(y(i:i-1+m));
end 

// Optimized solution with vectorization:
[X,Y] = meshgrid(1:m,1:n-m+1);
y2 = f(matrix(y(X+Y-1),n-m+1,m));

printf("difference = %g\n",norm(y1-y2'))

// END OF CODE

Regards,
Rafael




--
View this message in context: http://mailinglists.scilab.org/efficient-calculation-for-a-moving-window-tp4035997p4036002.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.



More information about the users mailing list