我有很多花车
x1 = [3418.008 3422.007 3424.007 3425.995 3428.026 3430.003 3539.979 3541.957
3650.041 3654.06 3656.062 3658.066 3660.019 3662.024 3664.024 3666.041
3667.985 3669.976 3672.009 3674.007 3767.969 3769.959 3771.959 3773.976
3775.975 3777.974 3779.97 3781.993 3784.002 3785.993 3787.991 3789.998
3791.997 3794.022 3882.009 3884.043 3886.036 3888.028 3890.007 3890.007
3892.015 3894.018 3895.983 3897.987 3903.978]
和一个不同长度的整数
x2 = [2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 ... 3936 3937 3938 3939 3940 3941].
它们都代表时间,这是由两种不同的仪器以不同的采样率测量的。x2还对应另一个长度相同的数组,
y2 =[ 9.000e-04 -1.000e-03 4.000e-04 -2.000e-03 1.000e-04 -1.600e-03 ... 1.200e-03 3.100e-03 -1.000e-03 6.000e-04]
(还包含零和负数)。这表示根据时间x2测得的可变量。
我想获得y2的修改版本,使其仅包含与x2值(最接近x1最近的元素)相对应的元素。如果最接近x1的x2元素为负,为零或它们的差大于epsilon,则y2_mod中的对应值也应删除。
我想这意味着对x2到x1应用某种蒙版,获取索引,或者对y2应用相同的蒙版?
编辑:我想我第一次就误解了这个问题,尝试一下。
要查找最接近的元素,可以np.meshgrid
将问题转换为二维并获得阵列之间的距离矩阵:
xx1, xx2, = np.meshgrid(x1, x2)
dist = np.abs(xx1 - xx2)
idx = dist.argmin(axis=0)
x2_mod = x2[idx] # same shape as y2_mod
y2_mod = y2[idx] # same shape as x2_mod
在这里,您可以基于负值和epsilon将屏蔽条件应用于阵列:
mask = (y2_mod >= 0) & (dist.min(axis=0) < eps)
y2_mod[mask]
根据您最近关于平均负值的评论:
# convolve your signal with an averaging kernel
kernel = [0.5, 0, 0.5]
avg = np.convolve(y2_mod, kernel, mode='same')
# apply boundary conditions (only consider one neighbor)
avg[[0, -1]] = y2_mod[[1, -2]]
# fill in the masked values
mask = y2_mod < 0
y2_mod[mask] = avg[mask]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句