vectorization - Can someone help vectorise this matlab loop? -
i trying learn how vectorise matlab loops, im doing few small examples.
here standard loop trying vectorise:
function output = moving_avg(input, n) output = []; n = n:length(input) % iterate on y vector summation = 0; ii = n-(n-1):n % iterate on x vector n times summation += input(ii); endfor output(n) = summation/n; endfor endfunction
i have been able vectorise 1 loop, cant work out second loop. here have got far:
function output = moving_avg(input, n) output = []; n = n:length(input) % iterate on y vector output(n) = mean(input(n-(n-1):n)); endfor endfunction
can me simplify further?
edit:
input 1 dimensional vector , maximum 100 data points. n single integer, less size of input (typically around 5)
i don't intend use particular application, simple nested loop thought use learn vectorisation..
matlab language type of operation poorly - require outside o(n) loop/operation involving @ minimum o(k) copies not worth in performance vectorize further because matlab heavy weight language. instead, consider using filter function these things typically implemented in c makes type of operation free.
Comments
Post a Comment