计算不同日间事件的频率

时间:2014-07-02 18:53:11

标签: matlab date matrix

假设我有一些矩阵,其中第一列是序列日期,第二列是该特定日期的信息。这些矩阵的组织方式是所有日期都是连续几天的最小数量。在下面的例子(A)中,这个数字是3.这就是说,A连续3天,连续4天,连续5天,等等,连续日数为3+。我的总矩阵范围从2+到5 +。

A=
694094   91 
694095   92 
694096   94  
694097   86
694157   95
694158   99
694159   99
694160   97
694183  100
694184   99 
694185   96 
694505   94
694506   92
694507   89
...

我想找到一种方法来计算每年不同的连续日数。也就是说,计算3天事件,4天事件以及之后的数量。因此,使用上面的示例,输出看起来像:

B=
1900 3
1901 1
....

其中指出1900年连续三天有3个以上的事件,根据例子,1901年只有一个连续3天的事件。这些年来自序列日期编号,可以找到相关的文档here。我的数据范围从1900年到2013年。

到目前为止,我已尝试使用diff函数尝试将日期事件按字符串中的1s分割,查找这些索引,然后使用histc计算每年的事件数但我意识到这是一种失败的方法。我确定accumarray可以在这种情况下提供帮助 - 但在通过mathworks和SO的示例后,我仍然对该函数有些模糊。

1 个答案:

答案 0 :(得分:2)

<强>代码

N = 3; %// 3 for 3+ events. Change it to 2 or 5 for 2+ and 5+ events respectively

%// Year IDs
year_ID = str2num(datestr(A(:,1),'yyyy')) 

%// Binary array, where ones represent consecutive dates starting with zero 
%// as the start of a pack of consecutive dates
diffA1 = [0 ; diff(A(:,1))==1]' %//'

%// Row numbers of A that signal the start of N+ events. 
%// STRFIND here works like a "sliding-matcher" if I may call it that way. 
%// It works with a matching window that slides across diffA1 to find N+ events 
%// using a proper filter. Here a filter [0 1 1] is used for 3+ events
row_ID = strfind(diffA1,[0 ones(1,N-1)]) 

%// N+ events for each year
Nplus_event = year_ID(row_ID)

%// Desired output as a count of such N+ events against each year
B = [unique(Nplus_event) histc(Nplus_event,unique(Nplus_event))]