我有数据文件: file 我需要进行X差分(第1列),但仅当第3列为1时。
import numpy as np x,jump= np.loadtxt("data.svc",delimiter=' ',skiprows=1, usecols=(0,3),unpack=True) resultX = list() i=0 while (i<len(jump)): if jump[i] == 1: while(jump[i] == 1): i+=1 temp = i resultX.append((abs(x[temp]-x[temp-1]))) i+=1 print(resultX)
我的结果是: 5,7,4,12,8,6,9,5,4,11 那是错误的
5,7,4,12,8,6,9,5,4,11
我需要: 5,7,4,8,6,5,4 correct results
5,7,4,8,6,5,4
我认为这会奏效:
>>> diff = np.diff(x) >>> diff array([ 5, 7, 4, 12, 4, 15, 8, 6, 9, 5, 5, 4, 5, 4, 11]) >>> >>> flag = np.diff(jump) + jump[1:] >>> flag array([ 1, 1, 1, -1, 0, 2, 1, 1, -1, 0, 0, 2, 1, 1, -1]) >>> >>> diff[flag == 1] array([5, 7, 4, 8, 6, 5, 4])