MATLAB:将行与每分钟事件相加

时间:2017-04-14 13:46:32

标签: matlab time time-series cell-array

在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]

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

1 个答案:

答案 0 :(得分:2)

您可以使用discretize结合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)];