Info

此问题已关闭。 请重新打开它进行编辑或回答。

Tell me when count of occurrences in a rolling 7-day range exceeds a certain value.

1 次查看(过去 30 天)
Say I have a column of dates and a column of ON and OFF representing the two states of a switch
5/16/2019 ON
5/16/2019 OFF
5/16/2019 ON
5/14/2019 OFF
5/13/2019 ON
5/13/2019 OFF
etc..
If I wanted to write a script that would tell me when the switch was OFF 4 times or more within ANY 7-day period (rolling period) how exactly would I go about that? It seems like it would be pretty simple but I'm not a good programmer whatsoever.
Thank you.

回答(1 个)

KSSV
KSSV 2019-5-17
T = readtable('data.txt') ;
S = T.(2) ;
on = contains(S,'ON') ;
off = contains(S,'OFF') ;
% Count ON
A = on' ;
ii = zeros(size(A));
jj = A > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',A(jj)',[],@(x){x'});
thesum_on = cellfun(@sum,out)
% Count OFF
A = off' ;
ii = zeros(size(A));
jj = A > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',A(jj)',[],@(x){x'});
thesum_off = cellfun(@sum,out)

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by