更新时间:2023-02-26 16:20:53
这是一种方法:
%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;
};
%转换为串行日期(忽略秒数部分)
dt = datenum(data(:,1),'yyyy-mm-dd HH:MM:SS');
%转换为组索引
[dt,〜,ind] = unique(dt);
%计算每个组
计数= accumarray(ind,cell2mat(data(:,2)),[],@numel);
%构造结果数据集
X = [cellstr(datestr(dt,'yyyy-mm-dd HH:MM:SS.FFF'))num2cell(计数)];
结果:
>> X
X =
'2001-03-23 05:01:33.000'[3]
'2001-03-23 05:01:34.000'[1]
我们不必转换为序列号,我们可以还执行了以下操作:
将列处理为char矩阵
dt = char(data(:,1) );
dt = dt(:,1:end-4); %删除分数秒
%唯一条目
[dt,〜,ind] = unique(dt,'rows');
%计数
计数= accumarray(ind,cell2mat(data(:,2)),[],@numel);
%result
X = [cellstr(strcat(dt,'。000'))num2cell(计数)];
I'd like to count entries per time interval.
Source dataset 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
...
Example output for group count by second:
2001-03-23 05:01:33.000,3
2001-03-23 05:01:34.000,1
...
Here is one way:
% 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)];
The result:
>> X
X =
'2001-03-23 05:01:33.000' [3]
'2001-03-23 05:01:34.000' [1]
We didn't have to convert to serial date numbers, we could have also done the following:
% 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)];