【问题标题】:Identify elements in a vector based on values from other vectors根据来自其他向量的值识别向量中的元素
【发布时间】:2018-06-29 22:54:10
【问题描述】:

我有 8 个事件的时间开始,称为 event1event2event3、...、event8(这些事件都不能同时发生,因此每个发作都是独一无二的)。每个事件都是 n x 1 double(n 可能因事件而异),如下所示:

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]

我想查找 event1 中紧接在 event2 中的时间点或 event4 中的时间点之前的时间点,这意味着之间不能有来自不同事件的另一个时间点。例如,事件 1 中的第一个时间点(即 29.7)符合此标准,因为它之前是 event4 中的第二个时间点(即 29.2)。类似地,event1的时间点4在event2的时间点4之前。相比之下,event1 中的第二个时间点(即 38.4)不符合条件,因为它之前是 event8 中的第一个时间点(即 38.2)。

结果应该是 event1 的索引,如下所示:index = [1;4]

谁能帮我解决这个问题?

【问题讨论】:

    标签: matlab indexing


    【解决方案1】:

    MATLAB 的内置函数可能会提供更简洁的解决方案,但我认为下面的解决方案很简单。

    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
    

    【讨论】:

    • 这很好用!!太感谢了。非常聪明!你拯救了我的星期五晚上。
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2022-08-23
    • 1970-01-01
    相关资源
    最近更新 更多