how to count values within a time range?

18 views (last 30 days)
Karin T Clausen
Karin T Clausen on 21 May 2015
Commented: Peter Perkins on 26 May 2015
Dear all,
I have a vector with time values (YYYY MM DD HH MM SS) (I can convert them using datenum and datestr) I need to get an output where the time period of 1 minute intervals is written in one column e.g. 2000 01 01 01:00:00, 2000 01 01 01:01:00...and the number of samples within this time period in the next column
There are unequal number of samples within a time period of 1 minute and I would like to count how many of my samples lies within ranges of 1 minute.
How do I do that?
  3 Comments
Karin T Clausen
Karin T Clausen on 22 May 2015
Dear Jan, I can convert them using datenum. The issue is that I have various files that each have a timevector (the different files have different sizes of the time vector). e.g.
time of event 1086x1 double
960x1 double
1255x1 double
These timevectors contain periods of an event that happened within 45 min period (and are down to seconds) and I have to group these events in time period of full minutes. Did that give enough information?
Guillaume
Guillaume on 22 May 2015
Edited: Guillaume on 22 May 2015
A date vector is a 1 x 6 vector where the first element is the year, the 2nd the month, etc: [Y M D H MN S]
Therefore it's not clear what you call a time vector. (I assumed it was an m x 6 matrix with each row a date vector). It could also be a cell array of date string, a vector of date numbers, a vector of datetime, or something else.
Please clarify. An example would be good. An array of double would seem to indicate that it is datenum.

Sign in to comment.

Answers (3)

Guillaume
Guillaume on 21 May 2015
Edited: Guillaume on 21 May 2015
Use unique with the 'rows' option to discretise your data into minutes (hence ignore the second column), then histcounts on the third return value of unique to find out the distribution for each minute:
%some random data for demo:
dt = [repmat([2000 1 1 1], 50, 1), randi([0 10], 50, 1), randi([0 59], 50, 1)]
[dtmin, ~, row] = unique(dt(:, 1:5), 'rows'); %ignore seconds (6th column)
nsamples = histcounts(row, [1:max(row) Inf]);
result = [dtmin, nsamples'] %6th column is sample distribution
  4 Comments
Guillaume
Guillaume on 21 May 2015
Edited: Guillaume on 21 May 2015
I don't see any problem with 1-minute intervals not being present. The solution uses whichever intervals are present.
Peter Perkins
Peter Perkins on 26 May 2015
The OP's goal may be to count up occurrences for all 1-minute interval, including returning zeros for intervals that are not present in the data. unique will not return anything for intervals not present in the data:
>> dt = [2015,1,1,1,1,0; 2015,1,1,1,1,30; 2015,1,1,1,1,45; 2015,1,1,1,3,0; 2015,1,1,1,3,30];
>> [dtmin, ~, row] = unique(dt(:, 1:5), 'rows');
>> nsamples = histcounts(row, [1:max(row) Inf]);
>> result = [dtmin, nsamples']
result =
2015 1 1 1 1 3
2015 1 1 1 3 2
What's missing is a row for minute number 2:
2015 1 1 1 2 0

Sign in to comment.


Jan
Jan on 21 May 2015
If you get the conversion to datenum to work, you can use something like this:
D = datenum(YourData); % However this works
D = floor(D * 1440);
[Value, Len, Index] = RunLength(D);

Peter Perkins
Peter Perkins on 21 May 2015
If you have R2014b or later, here's another possibility using datetime:
% set up some random data
>> y = 2015; mo = 1; d = 1;
>> h = 0; mi = randi([0 4],100,1); s = 60*rand(size(mi));
>> d = datetime(y,mo,d,h,mi,s);
% count number in each 1-minute interval
>> timeStep = datetime('1-Jan-2015 00:00:00') + minutes(0:4)';
>> [~,bin] = ismember(dateshift(d,'start','minute'),timeStep);
>> count = histcounts(bin,1:6)';
% create a table of the results
>> table(timeStep,count)
ans =
timeStep count
____________________ _____
01-Jan-2015 00:00:00 21
01-Jan-2015 00:01:00 15
01-Jan-2015 00:02:00 25
01-Jan-2015 00:03:00 19
01-Jan-2015 00:04:00 20
  2 Comments
Karin T Clausen
Karin T Clausen on 22 May 2015
Thank you very much for your answers are currently trying the different methods. yesterday I did not get it to work but I'll try this new suggestion by Peter.
Guillaume
Guillaume on 22 May 2015
If an answer does not work, says so in a comment to the answer and explain why it does not work.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!