Question regarding the error in coding a running mean

1 回表示 (過去 30 日間)
Nimrod
Nimrod 2012 年 6 月 13 日
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;

採用された回答

Nimrod
Nimrod 2012 年 6 月 13 日
Ok this is what I got so far from what I understood:
T=[22 24 16.2 8.4 -0.3 -7.3 -20.3 -37.1 -56.7];
%Returns an n by n maxtrix of zeros . An error message appears if n is not %scalar Tmean =zeros(7);
for k = 2 : length(T) - 1
Tmean = conv(T, ones(1,3)/3);
end
k_axis=2:1:9; Tmean(3:end-2)
plot(k_axis,Tmean) figure(1)
Of course what do I do with the k_axis delete that? And Plot what?
Thanks for the help.
  6 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 6 月 14 日
conv(T,ones(1,3)/3,'valid')
Walter Roberson
Walter Roberson 2012 年 6 月 14 日
In R2008b, only two parameters can be passed to conv(). The R2008b conv2() recognizes 'valid' though.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 6 月 13 日
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
Nimrod 2012 年 6 月 13 日
Error using plot Vectors must be the same lengths.
Error in Sample_Temperature_Profile (line 9) plot(2: length(Tmean_valid)- 1, Tmean_valid)
By the way if you do this:
T=[22 24 16.2 8.4 -0.3 -7.3 -20.3 -37.1 -56.7]; Tmean = conv(T, ones(1,3)/3); Tmean_valid = Tmean(3:end-2); plot( 2 : length(Tmean_valid) - 1, Tmean_valid)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by