find frequency and duration of numerical events in a matrix
2 ビュー (過去 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
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?
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!