find frequency and duration of numerical events in a matrix

조회 수: 3 (최근 30일)
elisabetta fedeli
elisabetta fedeli 2019년 3월 5일
편집: Andrei Bobrov 2019년 3월 7일
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
elisabetta fedeli 2019년 3월 6일
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
elisabetta fedeli 2019년 3월 6일
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?
Andrei Bobrov
Andrei Bobrov 2019년 3월 6일
I'm fixed my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (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