Given a vector a of N elements a n 1 2 N The simple moving
Given a vector a_ of N elements a_ n = 1, 2, ..., N. The simple moving average of m sequential elements of this vector is defined as mu_j = mu_j-1 + a_m+j-1 - a_j-1/m j = 2, 3, ..., (N-m+1), where mu_1 = 1/m sigma_k=i^m a_k Write a script that computes these moving averages when a is given by a = 5 * (1 + rond(N, 1)), where rand generates uniformly distributed random numbers. Assume that N=100 and m=6. Plot the results using plot (j, mu) for j = 1, 2..., N - m + 1.
Solution
N = 100;
m = 6;
a = 5 * (1 + rand(N, 1));
u = [];
u(1) = (1.0 / m) * (sum(a));
for j = 2:N-m+1
u(j) = u(j - 1) + ((a(m+j-1) - a(j-1)) / m);
end
plot(1:N-m+1, u)
