Homework Question Week 5 Question 8

Having problem with this question.
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.
This is my attempt
function av = moving_average(x)
persistent buffer
if isempty(buffer)
buffer = x;
else length(buffer)
buffer = buffer(end+1);
else if numel(buffer) > 25
buffer = buffer(end-24:end);
end
av = mean(buffer);
end
Doesn't even run correctly for argument 1. I welcome all guidance, help, advice or critique.

2 comentarios

Cedric
Cedric el 8 de Ag. de 2015
Editada: Cedric el 8 de Ag. de 2015
You cannot create an IF statement with multiple ELSE clauses. The correct way to do it is to use ELSEIF:
if condition1
...
elseif condition2
...
elseif condition3
...
else
...
end
At this stage I would give you the following advices:
  • Indent your code better so you see the structure in one sight.
  • Think about how you would solve the problem by hand, without programming, until you have a full understanding of the logic/algorithm that you should implement.
  • Write in English first (or in pseudo code) until the logic is meaningful and corresponds to how you would solve the problem by hand.
The problem when we learn programming is often that we directly try thinking in code, on a keyboard, even if we have no clue about the logic/algorithm for solving the problem. To be honest, even experienced programmers have to fight against their propensity to jump on a keyboard before they know what to do ;-)
Once you have a clear understanding, and only at this stage, you try to convert your algorithm from English to e.g. MATLAB.
Here is an example that applies to your case, where I combine a little bit of code that defines the general structure, and a description in English of the rest (this is in a sense what I called "pseudo-code" above):
function av = moving_average(x)
persistent buffer ;
if buffer is empty
set buffer = x
elseif buffer is smaller than 25 elements
concatenate buffer and x
else (meaning buffer has 25 elements)
concatenate buffer and x
remove first element
end
compute av = mean of elements in buffer
end
See, once you have that, you can think about the approach, maybe refine it and combine cases, and when you are happy with the logic you start converting it into MATLAB code. This conversion is a technical operation; you just have to translate into code. The part that requires intelligence is what you just did above, and it is good to keep these two parts of the development separate, so the technical aspects don't blur or interfere with your thinking.
This way of developing is what we do in fact at our level; the difference being that we write these sentences in English as comments, while we program (even if we shouldn't!). I have to emphasize that comments are often (meaning always minus a few cases) more important than the code, because they explain the logic, provide clarifications, arguments, etc, whereas the code "does the job" but is not that explicit. If you read code only, you will often think "ok, they do that, but why?". To finish on that matter, I'd say that it is not rare to see M-Files (in our context) that contain 1/3 of code and 2/3 of comments, and this can easily hit more than 90% of comments.
Emily Lim
Emily Lim el 8 de Ag. de 2015
Thank you very much for your advice.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Ag. de 2015
The statement
else length(buffer)
evaluates length(buffer) and does nothing with the calculation. Considering the structure of your code perhaps you wanted an "elseif" and wanted to compare the length to something.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Ag. de 2015

Comentada:

el 8 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by