calculate time from alarm (variabel x) to event (variable y)
1 次查看(过去 30 天)
显示 更早的评论
I want to calculate the time from an 'alarm' variable to the 'event' variable. In my dataset there are multiple alarms and multiple events. The time only needs to be calculated between an alarm and a direct subsequent event.
I am trying to go through my alarm column with a (for i) loop and try to find the event that comes after alarm(i). But I clearly need some help, this is where I am at:
time = [0;10;20;30;40;50;60;70;80;90;100]; % in sec; variables are generated every 10 seconds
alarm = [0;0;1;0;1;0;0;1;0;0;0];
event = [1;1;0;0;1;0;1;0;0;0;0];
dataset = table(time,alarm,event);
for i = 1:length(time)
if alarm(i) == 1 % looping through the alarms from i=1 to i=11
if (find(abs(dataset.event) > 0, 1, 'first') > i) % if the first event comes after alarm(i)
mark = find(abs(dataset.event) > 0, 1, 'first') > i);
time_event = dataset.time(mark); % return timepoint from corresponding row
else
% mark the first event after row alarm(i) && unless a new alarm occured before the next event
end
end
% time_to_event(i) = time_event - dataset.time;
end
% sum(time_to_event) = sum(time_to_event)
The line "find(abs(dataset.event) > 0, 1, 'first') > i)" is redundant if I knew how to target the row after alarm(i)..
So the output in my dataset should be as follows:
alarm = [0;0;1*;0;1^;0;0;1;0;0;0];
event = [1;1;0;0;1*;0;1^;0;0;0;0];
The time between the alarm* to event* and alarm^ to event^ should be calculated.
And after the loop is done, I want to sum all time to events.
Hope you can help
0 个评论
采纳的回答
Les Beckham
2022-4-22
Try this
t = [0;10;20;30;40;50;60;70;80;90;100]; % in sec; variables are generated every 10 seconds
alarm = [0;0;1;0;1;0;0;1;0;0;0];
event = [1;1;0;0;1;0;1;0;0;0;0];
alarms = find([ false; (diff(alarm) > 0) ]); % detect rising edges
events = find([ false; (diff(event) > 0) ]);
% you have to have both an alarm and an event to measure the time - how
% many do we have?
num_timed_events = min(numel(alarms), numel(events));
delaytimes = zeros(1, num_timed_events); % preallocate
for i = 1:num_timed_events
delaytimes(i) = t(events(i)) - t(alarms(i));
end
delaytimes
4 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!