Info
This question is closed. Reopen it to edit or answer.
Tell me when count of occurrences in a rolling 7-day range exceeds a certain value.
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
KSSV
on 17 May 2019
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)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!