How do I smooth a dataset without built in functions
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513612/image.png)
2 comentarios
Walter Roberson
el 9 de Feb. de 2021
Editada: Walter Roberson
el 9 de Feb. de 2021
You used indexing, but indexing is a built-in function.
You used addition, but addition is a built-in function.
You used division, but division is a built-in function.
Respuestas (2)
Chad Greene
el 9 de Feb. de 2021
Editada: Chad Greene
el 9 de Feb. de 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>;
end
3 comentarios
Image Analyst
el 9 de Feb. de 2021
That's pseudocode. You're supposed to replace that part with your own code. We're assuming it's homework and that you'd get in trouble for cheating if you were to turn in Chad's solution pretending it is your own. Is that correct? Is it homework? Are you allowed to turn in the solutions of others as your own?
Chad Greene
el 9 de Feb. de 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);
plot(x,y)
hold on
plot(x,ym)
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;
end
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;
end
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.
Dillon Romans
el 9 de Feb. de 2021
1 comentario
Walter Roberson
el 9 de Feb. de 2021
Hint:
format long g
y = factorial(1:15)
V = y(8-3:8+3)
mean(V)
Ver también
Categorías
Más información sobre LaTeX en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!