算在MATLAB每时间间隔的条目(Count entries per time interval i

2019-10-18 15:38发布

我想计算每个时间间隔的条目。

源数据集NX2

2001-03-23 05:01:33.347,55
2001-03-23 05:01:33.603,62
2001-03-23 05:01:33.977,32
2001-03-23 05:01:34.126,30
...

例如输出由第二组计数:

2001-03-23 05:01:33.000,3
2001-03-23 05:01:34.000,1
...

Answer 1:

这里有一种方法:

% dataset
data = {
  '2001-03-23 05:01:33.347', 55 ;
  '2001-03-23 05:01:33.603', 62 ;
  '2001-03-23 05:01:33.977', 32 ;
  '2001-03-23 05:01:34.126', 30 ;
};

% convert to serial date (ignoring the seconds fraction part)
dt = datenum(data(:,1), 'yyyy-mm-dd HH:MM:SS');

% convert to group indices
[dt,~,ind] = unique(dt);

% count occurences per group
counts = accumarray(ind, cell2mat(data(:,2)), [], @numel);

% construct resulting dataset
X = [cellstr(datestr(dt, 'yyyy-mm-dd HH:MM:SS.FFF')) num2cell(counts)];

结果:

>> X
X = 
    '2001-03-23 05:01:33.000'    [3]
    '2001-03-23 05:01:34.000'    [1]

我们没有转换成串行日期数字,我们也可以做如下:

% treat column as a char matrix
dt = char(data(:,1));
dt = dt(:,1:end-4);   % remove fractions of seconds

% unique entries
[dt,~,ind] = unique(dt, 'rows');

% counting
counts = accumarray(ind, cell2mat(data(:,2)), [], @numel);

% result
X = [cellstr(strcat(dt,'.000')) num2cell(counts)];


文章来源: Count entries per time interval in MATLAB