find frequency and duration of numerical events in a matrix

3 次查看(过去 30 天)
I have a matrix where the rows are minutes of a day (starting from 0:00 and ending at 23:59) and the columns are different days (over 1000 days). The numerical values in the matrix are events that occur over several minutes in a day so they are groups of values that extend over some rows in the same column surrounder by zeros. I would like to know, for every hour (es from 0:00 to 1:00) the mean value of the number of events that occurred and their mean duration in minutes.
  4 个评论
dpb
dpb 2019-3-5
OK, so we are clear on the definition of "event'...for the first column
2.50
0.00
0.29 -- begin?
0.50 or --begin?
0.50
0.50
0.50
0.22 -- end?
0.00 -- end??
0.00
...
is the event the any stretch of nonzero values or the stretch of equi-valued values?
Is the duration of the even inclusive/exclusive of the start/stop markers on one/both/either end?
elisabetta fedeli
Thank you for the reply, the event is any stretch of nonzero values.
2.50 -- begin and end (duration 1 minute)
0.00
0.29 -- begin
0.50
0.50
0.50
0.50
0.22 -- end (duration 6 minutes)
0.00
0.00
...
It would also be ideal if I could consider an event overlapping 2 hours a fraction of an event. Like this:
2.50 -- begin and end (duration 1 minute)
0.00
0.29 -- begin
0.50 --- end (the event is considered 2/6 of an event, duration 2 minutes)
----------- end of the first hour (every hour is composed of 60 rows)
0.50 --- begin
0.50
0.50
0.22 -- end (the event is considered 4/6 of an event, duration 4 minutes)
0.00
0.00
...

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-3-6
编辑:Andrei Bobrov 2019-3-7
Let A - your array (1440 x Days):
k = 60;% k = 60 (minuties in hour)
[m,n] = size(A);
a1 = A ~= 0;
A1 = diff([false(1,n);a1]) == 1;
[ii,iii] = ndgrid(1:k,1:m/k);
A1(ii(:) == 1 & a1) = true;
data = cumsum(A1).*double(a1);
T = array2table([iii(:),data],'V',[{'Time'},sprintfc('DAY%d',1:n)]);
out = varfun(@fun,T,'InputVariables',2:n+1,'GroupingVariables',1);
out = out(:,[1,3:end]);
out.Properties.VariableNames{1} = 'Hour';
Here fun is m-file fun.m:
function y = fun(x)
[~,~,c] = unique(x(x > 0));
y = [max(c), mean(accumarray(c,1))];
end
  2 个评论
elisabetta fedeli
Thank you very much! It works but i'm not sure it serves my purpose or maybe I just don't understand the script. I'm very new to matlab and i understand that every element in a column of the out matrix is an event and it's value is it's duration. Is it possible to group the results by hour? To have an out matrix 24 x n.days so that if in an hour there's no event the value for that hour is zero?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by