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.
댓글 수: 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)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!