find frequency and duration of numerical events in a matrix

3 views (last 30 days)
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 Comments
dpb
dpb on 5 Mar 2019
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
elisabetta fedeli on 6 Mar 2019
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
...

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 6 Mar 2019
Edited: Andrei Bobrov on 7 Mar 2019
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 Comments
elisabetta fedeli
elisabetta fedeli on 6 Mar 2019
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests 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!