function for calculating moving average

Write a function called moving_average that takes a scalar called x as an input argument and returns a scalar. The value it returns depends not only on the input but also on previous inputs to this same function when the function is called repeatedly. That returned value is a “moving average” of those inputs. The function uses a “buffer” to hold previous inputs, and the buffer can hold a maximum of 25 inputs. Specifically, the function must save the most recent 25 inputs in a vector (the buffer) that is a persistent variable inside the function. Each time the function is called, it copies the input argument into an element of the buffer. If there are already 25 inputs stored in the buffer, it discards the oldest element and saves the current one in the buffer. After it has stored the input in the buffer, it returns the mean of all the elements in the buffer. Thus, for each of the first 24 calls to the function, the function uses only the inputs it has received so far to determine the average (e.g., the first call simply returns x, the second call averages x and the input from the first call, etc.), and after that, it returns the average of the most recent 25 inputs.

Answers (1)

Hints: persistent, isempty(), and mean(). To "discard" an element:
buffer(index) = []; % Get rid of element at "index"

7 Comments

Up to now I have come up with this idea
function av = moving_average(x)
A=0;
if i>=0 && i<=25
buffer (i)=A;
y=sum (A);
av = A/n;
end
else %when i>25
buffer(i) = (i-26);% to store 26 on 1
%store 27 on 2
y=sum(A);
av = A/n;
end
I can't figure out how to sum the value of buffer and store them on A. Rest of error could be remove once I figure a way to solve my problem
You're not using persistent like it asked for. And you have to decide what you want buffer to hold: x or indexes. And what the heck is A? Why are you even using A at all? You might consider
buffer(end+1) = x; % Tack on new value
% See if it's longer than 25
% If it is, remove first (oldest) element
% so that we will always be no longer than 25.
if length(buffer) > 25
buffer(1) = []; % Remove first element.
end
I haven't used persistent previously so I don't know how to use it. I have tried to used it but can't get correct answers.
Besides that I am trying to save the value of buffer on a vector A so that I can use sum to add the value of A.
As average is (Sum of Buffer value)/25
  1. Don't use A.
  2. Don't use y.
  3. Don't use n.
  4. Make buffer persistent. Follow the directions in the help for "persistent".
  5. Don't use sum(). Use mean() instead and assign it to av.
  6. The only variables you need are x, av, and buffer, along with the length() and isempty() functions.
I've done virtually the whole thing for you except for 5 lines (besides the main function line). And I've told you that one of the lines is "persistent" and another is to assign the mean to av. The other 3 you will see in the help for persistent. If I do anymore for you, you're at risk of not doing your own work. Instructors/professors vary in how strict they are about that, so be cautious.
function y = moving_average(x)
persistent add cnt circBuff;
if isempty(add)
add = 0;
cnt = 0;
buffSize = 25;
circBuff = zeros(1,buffSize);
end
if cnt<25
circBuff = [circBuff(2:end) x]
S = sum(circBuff);
cnt = cnt + 1;
y = S / cnt
else
circBuff = [circBuff(2:end) x]
S = sum(circBuff);
y=S/25;
end
I used this code and get correct answers. My question is why is it mandatory to use isempty even I don't need it for calculation.
It is not mandatory or even advised. Who said anything about add? Plus it's obvious you're totally ignoring all my recommendations and doing it your own, much more complicated, way with a bunch of unneeded code. Good luck though.
Everyone has his own way of thinking I took your advice and whatever code come into my mind I used it. Don't take it personal.

Sign in to comment.

Categories

Asked:

on 17 May 2015

Commented:

on 18 May 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!