How to Implement the moving average filter in time domain in MATLAB? I have a sinusoidal x(t) = sin(500πt) signal  is corrupted by random noise. The corrupted signal is sampled with sampling frequency fs = 5 kHz and passed through a 5-point moving average filter to reduce the noise. I have written the following MATLAB Code for the same:-
fs=5e3; t=0:1/fs:0.02;
x=sin(2*pi*250*t);           % uncorrupted signal

r=0.1*randn(1,length(x));    % random noise
XR=x+r;                      % signal + noise (filter input)

y=0;
% Implement the moving average filter in time domain
for n=5:length(XR),          % M = 5   
    y(n) =                   % missing code using the command 'sum'
end

figure(12);
subplot(211); plot(t,XR,t,x,'r'); legend('Corrupted','Original');
xlabel('time (sec)'); ylabel('Amplitude'); grid;

subplot(212); plot(t,y,'k',t,x,'r'); legend('After filtering','Original'); 
xlabel('time (sec)'); ylabel('Amplitude');grid;

But, I am not sure how to implement the moving average filter in time domain using sum command to get the output (y[n]) in the for loop as mentioned above in the code. Any suggestions would be highly appreciated! Thanks a lot for the help
HINT (really an answer but not providing code so its a hint):
In your setup, a $M$-point moving average filter uses the average of the previous $M-1$ samples and the current sample to compute the current output. So for $n=5$, the output will be $\frac{1}{5}(x[1]+x[2]+x[3]+x[4]+x[5])$. In general, for $n=k$ the output of this $M$-point MA filter is:
$$ \frac{1}{M}\sum_{n=k-M+1}^kx[k]$$