2017-04-14 53 views
1

事件在MATLAB(R2015b)我有一個大的時間序列該單元格數據:MATLAB:和行到每分鐘

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

我該怎麼做我的時間序列?

回答

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)]; 
+0

謝謝你的幫助!我仍然得到「使用離散化的錯誤,太多的輸出參數。」當我在MATLAB中使用'help discretize'時,它只會顯示'BINS = discretize(X,EDGES)'的例子。我能以某種方式解決這個問題嗎或者我的版本(R2015b)太舊了? – litmus

+1

@litmus查看更新。 – Suever

+0

它完美的工作!謝謝! (以及我更新到新版本的時間) – litmus