how to count values within a time range?
18 views (last 30 days)
Show older comments
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
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.
Answers (3)
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
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
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);
0 Comments
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
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.
See Also
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!