1

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
...
Amro
  • 123,847
  • 25
  • 243
  • 454
John
  • 655
  • 2
  • 6
  • 12
  • 3
    Please edit your question to add what you have tried so far. We can help you solve your programming problems, we can't do your homework. – Simon Sep 15 '13 at 19:28

1 Answers1

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)];
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Hello Amro, could u please advise the following question http://stackoverflow.com/questions/23840437/data-analysis-in-time-interval –  May 24 '14 at 01:46
  • @user1724168: Have a look, I posted a solution (a modification of the above one). – Amro May 24 '14 at 13:46