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
Jan
2015년 5월 21일
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
2015년 5월 22일
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.
답변 (3개)
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
Karin T Clausen
2015년 5월 21일
Peter Perkins
2015년 5월 21일
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.
Peter Perkins
2015년 5월 26일
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
2015년 5월 21일
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
2015년 5월 21일
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
Karin T Clausen
2015년 5월 22일
Guillaume
2015년 5월 22일
If an answer does not work, says so in a comment to the answer and explain why it does not work.
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!