MATLAB Answers

How do I smooth a dataset without built in functions

28 views (last 30 days)
Dillon Romans
Dillon Romans on 9 Feb 2021
Commented: Chad Greene on 9 Feb 2021
I'm trying to run my data set through my function (y1 & x1) and then plot the new data. But when I run my code, the new plotted data is empty.
Dillon Romans
Dillon Romans on 9 Feb 2021
I apologize, I should of said without built in smoothing functions.

Sign in to comment.

Answers (2)

Chad Greene
Chad Greene on 9 Feb 2021
Edited: Chad Greene on 9 Feb 2021
It's not quite empty--it's just plotting a single point as a tiny dot. That's because you've only solved for a single point.
It looks like you're trying to do a five-point moving mean. You did the five-point mean part perfectly, but you forgot to move it! So try using a for loop to calculate the five-point moving mean for each point in the y vector. (You might need to skip the first and last few points because the moving window would need to start before the first index and end after the last index.)
Here's a start:
% Preallocate a vector for the smoothed y:
ym = nan(size(y));
% Calculate the moving mean for each datapoint:
for k = 3:(length(y-2))
ym(k) = <the mean of a 5 point window>;
Chad Greene
Chad Greene on 9 Feb 2021
It's no problem to use the forum for help with homework. I certainly identify with the feeling of having no clue where to start.
If you could use built-in functions, the solution would simply be
ym = movmean(y,5);
hold on
legend('raw data','smoothed data')
Your goal is to calculate ym without using the movmean function. How do you do that? Start by preallocating a vector the same size as y, like this:
ym = nan(size(y));
Then loop through each timestep, replacing each empty element with the mean of the y datapoints within a 5 point window. For example, if you wanted to populate every element in ym with a random number between 0 and 300, you might do
for k = 1:length(ym)
ym(k) = 300*rand;
If instead you wanted to populate each element of ym with the corresponding value of x squared, you might do
for k = 1:length(ym)
ym(k) = x(k)^2;
The challenge for you is to think about how you might populate each element in ym with the mean of the surrounding points. I'll give you a hint that you probably have to start with k=3 and you can't quite go all the way to the full length of ym if you're doing a 5 point moving mean.

Sign in to comment.

Dillon Romans
Dillon Romans on 9 Feb 2021
Yes its homework, but I'm not tyring to cheat or copy. If I can see how it works I can learn from it and hopefully make some progress on this. I've been trying to figure out how to get my code to work for a few hours but nothing is working.
  1 Comment
Walter Roberson
Walter Roberson on 9 Feb 2021
format long g
y = factorial(1:15)
y = 1×15
1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 6227020800 87178291200 1307674368000
V = y(8-3:8+3)
V = 1×7
120 720 5040 40320 362880 3628800 39916800
ans =

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by