似乎應該有一個numpy函數來查找兩個向量的重疊,但我似乎無法找到它。也許你們中的一個知道它?Numpy函數來查找重疊向量的索引
這個問題最好用一個簡單的代碼來描述(下面)。我有兩組數據(x1,y1)和(x2,y2),其中每個x和y是數百個元素。我需要將它們全部截斷以使域相同(即x1 = x2),並且y1表示與新x1一起使用的適當範圍,y2也將被截斷以與新x2一起移動。
# x1 and y1 are abscissa and ordinate from some measurement.
x1 = array([1,2,3,4,5,6,7,8,9,10])
y1 = x1**2 # I'm just making some numbers for the ordinate.
# x2 and y2 are abscissa and ordinate from a different measurement,
# but not over the same exact range.
x2 = array([5,6,7,8,9,10,11,12,13])
y2 = sqrt(x2) # And some more numbers that aren't the same.
# And I need to do some math on just the portion where the two measurements overlap.
x3 = array([5,6,7,8,9,10])
y3 = y1[4:10] + y2[:6]
# Is there a simple function that would give me these indices,
# or do I have to do loops and compare values?
print x1[4:10]
print x2[:6]
# ------------ THE FOLLOWING IS WHAT I WANT TO REPLACE -------------
# Doing loops is really clumsy...
# Check which vector starts lower.
if x1[0] <= x2[0]:
# Loop through it until you find an index that matches the start of the other.
for i in range(len(x1)):
# Here is is.
if x1[i] == x2[0]:
# Note the offsets for the new starts of both vectors.
x1off = i
x2off = 0
break
else:
for i in range(len(x2)):
if x2[i] == x1[0]:
x1off = 0
x2off = i
break
# Cutoff the beginnings of the vectors as appropriate.
x1 = x1[x1off:]
y1 = y1[x1off:]
x2 = x2[x2off:]
y2 = y2[x2off:]
# Now make the lengths of the vectors be the same.
# See which is longer.
if len(x1) > len(x2):
# Cut off the longer one to be the same length as the shorter.
x1 = x1[:len(x2)]
y1 = y1[:len(x2)]
elif len(x2) > len(x1):
x2 = x2[:len(x1)]
y2 = y2[:len(x1)]
# OK, now the domains and ranges for the two (x,y) sets are identical.
print x1, y1
print x2, y2
謝謝!
'numpy.in1d(X1,X2)'?我不知道你的問題或這些事情如何重疊?但是這段代碼會給你'[5,6,7,8,9,10]' –
它給了我一個True和False的向量。然後我必須通過第一個向量來找到第一個真實值,並將其設置爲我的偏移量,然後在第二個向量中找到相同的位置。 – ZSG
err看到答案(這可能是一個更好的解決方案)...我想這將是'x1 [numpy.in1d(x1,x2)]' –