Identify elements in a vector based on values from other vectors


Identify elements in a vector based on values from other vectors



I have time onsets for 8 events, called event1, event2, event3, ..., event8 (none of these events can occur simultaneously and thus each onset is unique). Each event is n x 1 double (n can vary from one event to another), like so:


event1 = [29.7; 38.4; 65.6; 149.6; 369.3]
event2 = [10.1; 11.8; 38.1; 142.2]
event3 = [5.5; 187.1; 200.1; 202.8; 236.7]
event4 = [15.3; 29.2; 54.5; 87.6; 100.4; 105.8; 120.9; 122.4]
...
event8 = [38.2; 66.7; 89.7; 100.5; 105.2; 168.9]



I want to find time points in event1 that are immediately preceded by either a time point in event2 or a time point in event4, meaning there cannot be another time point from a different event in between. For instance, the first time point in event 1 (i.e., 29.7) fits this criterion because it is preceded by the the second time point in event4 (i.e., 29.2). Similarly, time point 4 of event1 is preceded by time point 4 in event2. In contrast, the second time point in event1 (i.e., 38.4) doesn't qualify since it is preceded by the first time point in event8 (i.e., 38.2).



The result should be an index of event1 like this: index = [1;4]


index = [1;4]



Could anyone help me with this problem?




1 Answer
1



MATLAB's built-in functions might provide a cleaner solution, but I figured the following one is easy enough.


event1 = [29.7; 38.4; 65.6; 149.6; 369.3];
event2 = [10.1; 11.8; 38.1; 142.2];
event3 = [5.5; 187.1; 200.1; 202.8; 236.7];
event4 = [15.3; 29.2; 54.5; 87.6; 100.4; 105.8; 120.9; 122.4];
event8 = [38.2; 66.7; 89.7; 100.5; 105.2; 168.9];


event_agg = [event1; event2; event3; event4; event8]; % events vector, aggregate
eve_numb = [ones(length(event1),1); % event number vector
2*ones(length(event2),1);
3*ones(length(event3),1);
4*ones(length(event4),1);
8*ones(length(event8),1)];

EVENTS_IND = [event_agg, eve_numb]; % aggregate events with indices

EVENTS_SORT = sortrows(EVENTS_IND); % agg. events w/indices, sorted by event times

hits_array = ; % store the event times that meet the specified condition
for iter = 2:length(EVENTS_SORT)
if EVENTS_SORT(iter,2) == 1 % if the event time at iter comes from event #1
if EVENTS_SORT(iter-1,2) == 2 || EVENTS_SORT(iter-1,2) == 4 % if the event time at iter-1 come from events #2 or #4
hits_array = [hits_array EVENTS_SORT(iter,1)];
end
end
end





This works great!! Thank you so much. Very clever! You've saved my Friday night.
– A.Rainer
Jun 30 at 1:40






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift