How do I smooth a dataset without built in functions

조회 수: 13 (최근 30일)
Dillon Romans
Dillon Romans 2021년 2월 9일
댓글: Chad Greene 2021년 2월 9일
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
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.
Dillon Romans
Dillon Romans 2021년 2월 9일
I apologize, I should of said without built in smoothing functions.

댓글을 달려면 로그인하십시오.

답변 (2개)

Chad Greene
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
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
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
Dillon Romans 2021년 2월 9일
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
Walter Roberson
Walter Roberson 2021년 2월 9일
Hint:
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
mean(V)
ans =
6279240

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 LaTeX에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by