作为标题,假设我得到了一个(n,2)numpy数组,该数组记录了一系列段的开始和结束索引,例如n = 6:
import numpy as np
# x records the (start, end) index pairs corresponding to six segments
x = np.array(([0,4], # the 1st seg ranges from index 0 ~ 4
[5,9], # the 2nd seg ranges from index 5 ~ 9, etc.
[10,13],
[15,20],
[23,30],
[31,40]))
现在,我想以较小的间隔合并这些段.例如,如果间隔不大于1,则合并连续的段,因此所需的输出将是:
y = np.array([0,13], # Cuz the 1st seg's end is close to 2nd's start,
# and 2nd seg's end is close to 3rd's start, so are combined.
[15,20], # The 4th seg is away from the prior and posterior segs,
# so it remains untouched.
[23,40]) # The 5th and 6th segs are close, so are combined
这样输出段就会变成三个,而不是六个.
任何建议,将不胜感激!