MATLAB: Vectors, Sequential Elements and rand -
this question chegg.com.
given vector of n elements a_{n},n =1,2,...,n, simple moving average of m sequential elements of vector defined as
mu(j) = mu(j-1) + (a(m+j-1)-a(j-1))/m j = 2,3,...,(n-m+1)
where
mu(1) = sum(a(k))/m k = 1,2,...,m
write script computes these moving averages when a
given a=5*(1+rand(n,1))
, rand
generates uniformly distributed random numbers. assume n=100
, m=6
. plot results using plot(j,mu(j))
j=1,2,...,n-m+1
.
my current code below, i'm not sure go here or if it's right.
close clear clc n = 100; m = 6; = 5*(1+rand(n,1)); mu = zeros(n-m+1,1); mu(1) = sum(a(1:m)); j=2 mu(j) = mu(j-1) + (a-a)/m end plot(1:n-m+1,mu)
i'll step through modifications.
firstly, mu(1)
not defined. equation given incorrect, should be:
mu(1) = sum(a(1:m))/m;
then for
loop has go j=2
j=n-m+1
for j=2:n-m+1
and @ each step, mu(j)
given formula, same given in question
mu(j) = mu(j-1) + (a(m+j-1)-a(j-1))/m
and that's need change!
matlab vector moving-average
No comments:
Post a Comment