Question regarding the error in coding a running mean
Ältere Kommentare anzeigen
Hello MATLAB users,
I was wonder this.
Need help in finding an easy and explainable code to find the running mean to filter out the data on MATLAB.? Need help in finding an easy and explainable code to find and plotting the running mean to filter out the data on MATLAB.
For example you have
T=[22 24 16.2 8.4 -0.3 -7.3 -20.3 -37.1 -56.7];
h=[0 914 1876 3141 4267 5830 7530 9590 12280];
The plan is mathmatically to do for example
you have K = 2-9 for 3 pts
Tf(K) = (T(k-1)+T(k)+T(k+1))/3
Anyway there is a clear and explainable MATLAB code? Thanks!
Is this approach I should be taking?
do a mean filter (kind of like median filter, except mean) with a window size == 3.
Tmean=zeros(7);
for k=2:1:9,
%assuming 10 elements, you need to cut it before the last one
Tmean(k)=(T(k-1)+T(k)+T(k+1))/3
end
k_axis=2:1:9
figure(1)
plot(k_axis, Tmean)
I got an error message stating:
Attempted to access T(10); index out of bounds because nume1(T)=9.
Error in Sample_Temperature_profile (line 12)
Tmean(k) = (T(k-1)+T(k)+T(k+1))/3;
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 13 Jun. 2012
for k = 2 : length(T) - 1
Otherwise when you are at k = length(T) then k+1 is length(T) + 1 which is out of range.
You have not indicated how you want your running mean to behave when you are at the beginning (where there is no point to the left) or at the end (where there is no point to the right.)
Consider using
Tmean = conv(T, ones(1,3)/3);
and then extracting Tmean(3:end-2)
Nimrod
am 13 Jun. 2012
0 Stimmen
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!