How do I smooth a dataset without built in functions
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-2-9
编辑:Walter Roberson
2021-2-9
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.
回答(2 个)
Chad Greene
2021-2-9
编辑:Chad Greene
2021-2-9
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 个评论
Image Analyst
2021-2-9
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
2021-2-9
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
2021-2-9
1 个评论
Walter Roberson
2021-2-9
Hint:
format long g
y = factorial(1:15)
V = y(8-3:8+3)
mean(V)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 LaTeX 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!