Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.
#import what we need for array operations
from numpy import array, concatenate, insert, delete, allclose
#first array
xs = array([
[59, 24.5, 25.5, 26.5, 4],
[1727, 21.5, 22.5, 23.5, 9],
[1840, 21.5, 22.5, 23.5, 9],
[2252, 22.0, 23.0, 24.0, 4],
[2445, 22.0, 23.0, 24.0, 4]
])
#second array
ys = array([
[159, 124.5, 125.5, 126.5],
[1227, 121.5, 122.5, 123.5],
[1340, 121.5, 122.5, 123.5],
[1452, 122.0, 123.0, 124.0],
[2945, 122.0, 123.0, 124.0]
])
# we walk on time, start from min timestamp to max timestamp
#x array contains last xs[xi] that we pass its time
x = array([])
#x array contains last ys[yi] that we pass its time
y = array([])
#contains un filtered result, i.e. contanins additional rows that are not needed
result = array([[0., 0., 0., 0., 0., 0., 0., 0.]])
#xi and yi are indices for xs and xy respectively
xi = 0
yi = 0
#here we start, until one of xs or ys array completely checked
while xi < len(xs) and yi < len(ys):
#if time stamp of xs is smaller than ys, then we update result array by inserting a new row to it containing timestamp, x and y
if xs[xi][0] < ys[yi][0]:
x = xs[xi][1:]
#this if checks that y is not empty(i.e for the first time)
if len(y) > 0:
# make new row with smallest timestamp x and y
r = concatenate(([xs[xi][0]] , x , y))
#insert the new row to the result array
result = insert(result, len(result), r, axis=0)
#increase xi by one
xi = xi + 1
else:
y = ys[yi][1:]
#this if checks that x is not empty(i.e for the first time)
if len(x) > 0:
# make new row with smallest timestamp x and y
r = concatenate(([ys[yi][0]] , x , y))
#insert the new row to the result array
result = insert(result, len(result), r, axis=0)
#increase yi by one
yi = yi + 1
#if xs does not completely checked, this while help us to check remaining items in it
while xi < len(xs):
x = xs[xi][1:]
r = concatenate(([xs[xi][0]] , x , y))
result = insert(result, len(result), r, axis=0)
xi = xi + 1
#and if ys does not completely checked, this while help us to check remaining items in it, note that only one of these two whiles executed!
while yi < len(ys):
y = ys[yi][1:]
r = concatenate(([ys[yi][0]] , x , y))
result = insert(result, len(result), r, axis=0)
yi = yi + 1
#remove row number i if its x and y is the same as row number i-1.
for i in reversed(range(1, len(result))):
if allclose(result[i][1:], result[i-1][1:]):
result = delete(result, i, axis=0)
#delete the first row i.e. [0.,0.,0.,0.....
result = delete(result, 0, axis=0)
print(result)
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Python On Mouseover Save URL/Link | 4 | 63 | |
C# Error - Add Failed | 12 | 87 | |
How to get time difference in minutes and seconds only between 2 dates | 2 | 41 | |
Removing line numbers from left column in config file | 7 | 52 |
Join the community of 500,000 technology professionals and ask your questions.