It seems like there should be a numpy function for finding the overlap of two vectors, but I can't seem to find it. Maybe one of you knows it?
The problem is best described with a simple code (below). I have two sets of data (x1, y1), and (x2, y2), where each x and y are hundreds of elements. I need to truncate them all so that the domains are the same (ie x1 = x2), and y1 represents the appropriate range to go with the new x1, y2 is also truncated to go with the new 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
Thanks!
For a simple intersection, you can use np.intersect1d
:
In [20]: x1 = array([1,2,3,4,5,6,7,8,9,10])
In [21]: x2 = array([5,6,7,8,9,10,11,12,13])
In [22]: x3 = np.intersect1d(x1, x2)
In [23]: x3
Out[23]: array([ 5, 6, 7, 8, 9, 10])
But it looks like you need something different. As @JoranBeasley suggested in a comment, you can use np.in1d
, but you need to use it twice:
Here's the data:
In [57]: x1
Out[57]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
In [58]: y1
Out[58]: array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100])
In [59]: x2
Out[59]: array([ 5, 6, 7, 8, 9, 10, 11, 12, 13])
In [60]: y2
Out[60]:
array([ 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ,
3.16227766, 3.31662479, 3.46410162, 3.60555128])
Get the subset of the (x1, y1) data:
In [61]: mask1 = np.in1d(x1, x2)
In [62]: xx1 = x1[mask1]
In [63]: yy1 = y1[mask1]
In [64]: xx1, yy1
Out[64]: (array([ 5, 6, 7, 8, 9, 10]), array([ 25, 36, 49, 64, 81, 100]))
Get the subset of the (x2, y2) data. Note that the order of the arguments to np.in1d
is now x2, x1
:
In [65]: mask2 = np.in1d(x2, x1)
In [66]: xx2 = x2[mask2]
In [67]: yy2 = y2[mask2]
In [68]: xx2, yy2
Out[68]:
(array([ 5, 6, 7, 8, 9, 10]),
array([ 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ,
3.16227766]))
We didn't really have to form xx2
, because it will be the same as xx1
. We can now operate on yy1
and yy2
. Eg:
In [69]: yy1 + yy2
Out[69]:
array([ 27.23606798, 38.44948974, 51.64575131, 66.82842712,
84. , 103.16227766])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.