所以我想构建一个不同月份不同权重的移动时间平均值。例如。请参阅http://www.mathworks.com/help/matlab/data_analysis/filtering-data.html处的过滤功能,其中 b = 每个月的天数,a = 一年的天数。
问题是,时间序列是每个月的一系列温度(我想为每组可能的年份构建年平均温度,其中一年可能是从三月到二月,对于例子)。使用这种方法,每个窗口中的第一个月的权重将为 31/365,无论第一个月是 1 月还是 6 月。
在这种情况下,标准过滤器算法将不起作用。有替代方案吗?
包含闰年的解决方案也很好,但对于初始解决方案来说不是必需的。
最佳答案
加权平均值定义为sum(x .* Weights)/sum(weights)
。如果你想以移动平均的方式计算这个,我想你可以这样做(未经测试):
moving_sum = @(n, x) filter(ones(1,n), 1, x);
moving_weighted_avg = moving_sum(12, temperature .* days_per_month) ...
./ moving_sum(12, days_per_month);
如果温度
是每月温度的向量,并且days_per_month
包含相应月份的实际天数,那么即使在闰年的情况下,这也应该有效。
编辑以回复评论
您可以像这样重建days_per_month
:
start_year = 2003;
start_month = 10;
nmonth = 130;
month_offset = 0:nmonth - 1;
month = mod(start_month + month_offset - 1, 12) + 1;
year = start_year + floor((start_month + month_offset - 1) / 12);
days_in_month = eomday(year, month);
disp([month_offset; year; month; days_in_month]') %print table to check
关于matlab - 如何构建不同月份不同权重的移动时间平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302678/