2014-03-12 53 views
0

我正在写for循环来平均每小时进行10年的小时测量。测量日期记录为MATLAB日期。在MATLAB中按小时迭代循环

我想通过使用0.0417进行迭代,因为它是1AM 00/00/00的datenum,但是每次迭代时都会在几秒钟内添加错误。

任何人都可以推荐一个更好的方法来让我按小时迭代吗?

date = a(:,1); 
load = a(:,7); 

%loop for each hour of the year 
for i=0:0.0417:366 

    %set condition 
    %condition removes year from current date 
    c = date(:)-datenum(year(date(:)),0,0)==i; 

    %evaluate condition on load vector and find mean 
    X(i,2)=mean(load(c==1)); 

end 

回答

2

一个钟头1/24一天,不0.0417的持续时间。使用1/24,一年的精度足够高。

要获得更高的精度,请使用类似datenum(y,1,1,1:24*365,0,0)的东西来生成所有时间戳。

+0

这么简单很明显,非常感谢。 – kekni

2

为了避免误差漂移完全,使用整数指定索引,上下分割循环内的结果:

for hour_index=1:365*24 
    hour_datenum = (hour_index - 1)/24; 
end