Home > data-analysis > manip > event > event_OR.m

event_OR

PURPOSE ^

event_OR Return the union set of two event data

SYNOPSIS ^

function res = event_OR(event1, event2)

DESCRIPTION ^

event_OR   Return the union set of two event data
   res = event_OR(event1, event2);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function res = event_OR(event1, event2)
0002 %event_OR   Return the union set of two event data
0003 %   res = event_OR(event1, event2);
0004 %
0005 res = event1;
0006 
0007 for i = 1:event_number(event2) % for each time interval in event2
0008     res = add_interval(res, event2(i,:));
0009 end 
0010 
0011 
0012 %time_interval_OR  Return the union of two time intrvals
0013 %
0014 function ti_OR = time_interval_OR(ti1, ti2)
0015 if check_intersection(ti1, ti2)
0016     ti_OR = [min([ti1(1) ti2(1)]), max([ti1(2) ti2(2)])];
0017 else
0018     if ti1(1) < ti2(1)
0019         ti_OR = [ti1; ti2];
0020     else
0021         ti_OR = [ti2; ti1];
0022     end
0023 end
0024 
0025 
0026 % Add the interval to the event (series of intervals)
0027 function new_event = add_interval(event, interval)
0028 if isempty(event)
0029     new_event = interval;
0030     return;
0031 end
0032 
0033 for i = 1:event_number(event)  % for each time interval in event
0034     ti = event(i,:); % the i-th time interval of the event
0035     if check_intersection(interval, ti)
0036         new_interval = time_interval_OR(interval, ti); % compute union of two interval
0037         new_event = update_event(event, i, new_interval);  % replace ti1 with new_interval in res
0038         break;
0039     elseif interval(2) < ti(1)
0040         new_event = [event(1:i-1,:); interval; event(i:event_number(event),:)];  % insert
0041         break;
0042     elseif i == event_number(event)     % if it is the last one
0043         new_event = [event; interval];  % insert at the end
0044         break;
0045     end
0046 end
0047     
0048 %Put the interval to i-th position of the event, and then
0049 %check if this interval intersect with those interval at the right of it
0050 function new_event = update_event(event, i, interval)
0051 merge_num = 0;
0052 n = event_number(event);
0053 for j=i+1:n
0054     ti = event(j,:);
0055     if check_intersection(interval,ti)
0056         interval = time_interval_OR(interval,ti);
0057         merge_num = merge_num+1;
0058     else
0059         break;
0060     end
0061 end
0062 event(i,:) = interval;
0063 
0064 if i+merge_num+1 > n
0065     new_event = event(1:i,:);
0066 else
0067     new_event = [event(1:i,:); event(i+merge_num+1:n,:)];
0068 end
0069 
0070 
0071 % check if two time intervals intersect with each other
0072 function flag = check_intersection(ti1, ti2)
0073 flag = ~(ti1(2)<ti2(1) || ti2(2)<ti1(1));

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005