how to count values within a time range?

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 comentarios

Jan
Jan el 21 de Mayo de 2015
Please tell us the class and the size of the "time vector (YYYY MM DD HH MM SS)". A small example in valid Matlab syntax would be useful. The statement, that you cannot convert the values by datenum is not sufficient to understand, what's going on. Why can't you convert them?
Karin T Clausen
Karin T Clausen el 22 de Mayo de 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 el 22 de Mayo de 2015
Editada: Guillaume el 22 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (3)

Guillaume
Guillaume el 21 de Mayo de 2015
Editada: Guillaume el 21 de Mayo de 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 comentarios

Karin T Clausen
Karin T Clausen el 21 de Mayo de 2015
Thanks Guillaume. I'll try it right away
Peter Perkins
Peter Perkins el 21 de Mayo de 2015
Depending on what you're expecting, you may run into trouble with this if some of your 1-minute intervals have no data in them. You may want to set up a matrix that includes all the 1-minute time steps, and use ismember against that, rather than unique.
Guillaume
Guillaume el 21 de Mayo de 2015
Editada: Guillaume el 21 de Mayo de 2015
I don't see any problem with 1-minute intervals not being present. The solution uses whichever intervals are present.
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

Iniciar sesión para comentar.

Jan
Jan el 21 de Mayo de 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 el 21 de Mayo de 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 comentarios

Karin T Clausen
Karin T Clausen el 22 de Mayo de 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 el 22 de Mayo de 2015
If an answer does not work, says so in a comment to the answer and explain why it does not work.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 21 de Mayo de 2015

Comentada:

el 26 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by