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

event_AND

PURPOSE ^

event_AND Return the intersection of two event data

SYNOPSIS ^

function res = event_AND(event1, event2)

DESCRIPTION ^

event_AND   Return the intersection of two event data

 Not safe for cevents!
 If you want that, try event_extract_ranges (returns events in a cell
 array, which is slightly different; use vertcat(chunks{:}) to get them
 back into one piece).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function res = event_AND(event1, event2)
0002 %event_AND   Return the intersection of two event data
0003 %
0004 % Not safe for cevents!
0005 % If you want that, try event_extract_ranges (returns events in a cell
0006 % array, which is slightly different; use vertcat(chunks{:}) to get them
0007 % back into one piece).
0008 %
0009 
0010 res = event_intersection(event1, event2); 
0011 if res == 0
0012     res = [];
0013 end
0014 
0015 if ~isempty(res)
0016     res = res(res(:,1)~=res(:,2),:);  % delete empty intervals
0017 end
0018 
0019 if isempty(res)
0020     res = [];
0021 end
0022 
0023 
0024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0025 % Get the overlappaed parts between two events
0026 % Input: Two events
0027 % :: consist of (event start time, event end time)
0028 % Output: The overlapped parts
0029 % Author: Seehyun Kim
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 function overlapped_event_part = event_intersection(vec1,vec2)
0033 
0034 index = 1; indexFirst = 1; indexSecond = 1;    
0035 element = [];
0036 
0037 while (indexFirst < (size(vec1,1)+1)) && (indexSecond < (size(vec2,1)+1))
0038     if (vec1(indexFirst,1) >= vec2(indexSecond,1)) && (vec1(indexFirst,1) <= vec2(indexSecond,2))
0039         if (vec1(indexFirst,2) >= vec2(indexSecond,1)) && (vec1(indexFirst,2) <= vec2(indexSecond,2))
0040             element(index,1) = vec1(indexFirst,1);
0041             element(index,2) = vec1(indexFirst,2);
0042             index = index + 1;
0043             indexFirst = indexFirst + 1;
0044         else
0045             element(index,1) = vec1(indexFirst,1);
0046             element(index,2) = vec2(indexSecond,2);
0047             index = index + 1;
0048             indexSecond = indexSecond + 1;
0049         end
0050     elseif (vec2(indexSecond,1) >= vec1(indexFirst,1)) && (vec2(indexSecond,1) <= vec1(indexFirst,2))
0051         if (vec2(indexSecond,2) >= vec1(indexFirst,1)) && (vec2(indexSecond,2) <= vec1(indexFirst,2))
0052             element(index,1) = vec2(indexSecond,1);
0053             element(index,2) = vec2(indexSecond,2);
0054             index = index + 1;
0055             indexSecond = indexSecond + 1;
0056         else
0057             element(index,1) = vec2(indexSecond,1);
0058             element(index,2) = vec1(indexFirst,2);
0059             index = index + 1;
0060             indexFirst = indexFirst + 1;
0061         end
0062     elseif vec1(indexFirst,1) > vec2(indexSecond,2)
0063         indexSecond = indexSecond + 1;
0064     else
0065         indexFirst = indexFirst + 1;
0066     end
0067 end
0068 
0069 if isempty(element) %|| element == 0
0070     overlapped_event_part = [];
0071 else
0072     r = find(element(:,1) ~= element(:,2));
0073     overlapped_event_part = element(r,:);
0074 end
0075 
0076

Generated on Wed 24-May-2017 17:00:59 by m2html © 2005