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

采纳的回答

Les Beckham
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
delaytimes = 1×2
20 20
  4 个评论
SRRellum
SRRellum 2022-4-24
Thanks, yeah you're right. Alarms in a row should be considered as the same alarm indeed.
However, in this dataset the 'alarms' are a predictive alarm that predict low blood pressure. Events are low blood pressure events. So if an alarm occurs I want to know how long before low pressure a prediction was provided. However, between a prediction and low blood pressure an intervention could have been given to the patient (the doctors were blinded to the alarms). That intervention prevents the low blood pressure from happening. So If an alarm occured in the dataset and then normalizes without a low blood pressure, then that alarm is not interesting to me, because it was altered. But the next alarm to an event is interesting.
But I will work with your solution for now. And see what it does in my real dataset.
Thanks a lot for your help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by