MATLAB:每分钟将行汇总为事件

MATLAB: Sum rows into events per minute

在 MATLAB (R2015b) 中,我有来自大型时间序列的单元格数据:

'01-Jan-2017 09:01:48'    [ 5]
'01-Jan-2017 09:01:50'    [ 2]
'01-Jan-2017 09:01:51'    [12]
'01-Jan-2017 09:01:53'    [ 2]
'01-Jan-2017 09:01:56'    [ 1]
'01-Jan-2017 09:02:00'    [ 1]
'01-Jan-2017 09:02:01'    [ 2]
'01-Jan-2017 09:02:12'    [ 1]
'01-Jan-2017 09:02:17'    [ 2]
'01-Jan-2017 09:02:19'    [ 1]
'01-Jan-2017 09:02:21'    [ 4]
'01-Jan-2017 09:02:52'    [ 1]
'01-Jan-2017 09:03:00'    [ 1]
'01-Jan-2017 09:03:05'    [ 3]
'01-Jan-2017 09:03:23'    [ 2]
'01-Jan-2017 09:03:26'    [ 3]
'01-Jan-2017 09:03:36'    [ 3]
'01-Jan-2017 09:03:37'    [ 2]
'01-Jan-2017 09:03:38'    [ 1]
'01-Jan-2017 09:03:43'    [ 2]
'01-Jan-2017 09:03:49'    [ 2]
'01-Jan-2017 09:03:51'    [ 1]
'01-Jan-2017 09:03:55'    [ 1]

但是,我想将这些行汇总为每分钟(而不是每秒)的事件,即

'01-Jan-2017 09:01:00'    [ 22]
'01-Jan-2017 09:02:00'    [ 12]
'01-Jan-2017 09:03:00'    [ 21]

如何为我的时间序列执行此操作?

您可以使用 discretize combined with accumarray 来汇总同一分钟内发生的所有值。首先,我们必须将日期字符串的第一列转换为 datetime 对象,然后执行第二列的求和,我们使用 [data{:,2}]

将其转换为数值数组
% Convert the first column to datetime objects and discretize by minute
[inds, edges] = discretize(datetime(data(:,1)), 'minute');

% Sum all values from the same minute
sums = accumarray(inds, [data{:,2}]);

% Create the output cell array of date strings and sums
result = [cellstr(datestr(edges(1:end-1))), num2cell(sums)];

%   '01-Jan-2017 09:01:00'    [22]
%   '01-Jan-2017 09:02:00'    [12]
%   '01-Jan-2017 09:03:00'    [21]

更新

因此看起来 discretize 与 R2015b 中的 datetime 对象不能很好地配合使用,但您可以执行如下操作,我们将日期分解为它们的组成部分,删除秒数,确定唯一组并再次使用 accumarray 执行求和

% Break each date into it's components
dv = datevec(data(:,1));

% Set the seconds to 0 so that only minutes are considered
dv(:,end) = 0;

% Find the unique minutes
[vals, ~, inds] = unique(dv, 'rows');

% Sum up the value for each unique minute
sums = accumarray(inds, [data{:,2}]);

% Create the output cell array
result = [cellstr(datestr(vals)), num2cell(sums)];