function ySmooth = slidingAvgSmooth(y, n, m)
% function ySmooth = slidingAvgSmooth(y, n, m)
% "Write a Matlab function that accepts a vector y and applies an n-point
% sliding average smooth to y and then repeats the smooth on the previously
% smoothed data m times." Trial 3
%  Initialize the smoothed vector with the original data
  ySmooth = y;
  
  % Iterate over the smoothing process
  for i = 1:m
    % Initialize the smoothed data for this iteration
    ySmoothIteration = zeros(size(ySmooth));
    
    % Loop over the elements in the vector
    for j = 1:length(ySmooth)
      % Calculate the start and end indices for the sliding window
      startIdx = max(j-n, 1);
      endIdx = min(j+n, length(ySmooth));
      
      % Calculate the average of the elements in the sliding window
      windowAvg = mean(ySmooth(startIdx:endIdx));
      
      % Store the average in the smoothed vector
      ySmoothIteration(j) = windowAvg;
    end
    
    % Update the smoothed vector with the new data
    ySmooth = ySmoothIteration;
  end
end
