Skip to main content
deleted 15 characters in body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
x = merge_timestamps([b, c, a]
merge_timestamps([a,b,c])

array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
       [107.,  13., 123., 119., 137., 135.,  65., 157.,  83.],
       [185.,   0., 117.,   0.,   0.,   0.,   0.,   0.,   0.],
       [ 81.,   0.,  49.,   0.,  83.,   0.,  32.,   0., 179.]])
x = [b,c,a]
merge_timestamps([a,b,c])

array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
       [107.,  13., 123., 119., 137., 135.,  65., 157.,  83.],
       [185.,   0., 117.,   0.,   0.,   0.,   0.,   0.,   0.],
       [ 81.,   0.,  49.,   0.,  83.,   0.,  32.,   0., 179.]])
merge_timestamps([b, c, a])

array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
       [107.,  13., 123., 119., 137., 135.,  65., 157.,  83.],
       [185.,   0., 117.,   0.,   0.,   0.,   0.,   0.,   0.],
       [ 81.,   0.,  49.,   0.,  83.,   0.,  32.,   0., 179.]])
Post Undeleted by yatu
added 15 characters in body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    A = x[0][max_len_list][0]x[0][max_len_list]
    out = np.vstack([A[A[1], np.zeros((len(*x)-1, len(AA[0])))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(*x[-2:]others):
        out[-(ix+1), i[0]x[0][i][0]-AA[0].min()] = i[1]x[0][i][1]
    return out
def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    A = x[0][max_len_list][0]
    out = np.vstack([A, np.zeros((len(*x)-1, len(A)))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(*x[-2:]):
        out[-(ix+1), i[0]-A.min()] = i[1]
    return out
def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    A = x[0][max_len_list]
    out = np.vstack([A[1], np.zeros((len(*x)-1, len(A[0])))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(others):
        out[-(ix+1), x[0][i][0]-A[0].min()] = x[0][i][1]
    return out
Post Deleted by yatu
Post Undeleted by yatu
Post Deleted by yatu
edited body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148

Here's a almost fully vectorised approach for a scenario with multiple b matrices. This approach does not require a priori knowledge of which is the largest list:

def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    A = x[0][max_len_list][0]
    out = np.vstack([a[A, np.zeros((len(*x)-1, a.shape[1]len(A)))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(*x[-2:]):
        out[-(ix+1), i[0]-x[0][max_len_list][0]A.min()] = i[1]
    return out

ThisAs mentioned this approach does not require a priori knowledge of which is the largest list, i.e. it would also work with:

Here's a almost fully vectorised approach for a scenario with multiple b matrices:

def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    out = np.vstack([a, np.zeros((len(*x)-1, a.shape[1]))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(*x[-2:]):
        out[-(ix+1), i[0]-x[0][max_len_list][0].min()] = i[1]
    return out

This approach does not require a priori knowledge of which is the largest list, i.e. it would also work with:

Here's a almost fully vectorised approach for a scenario with multiple b matrices. This approach does not require a priori knowledge of which is the largest list:

def merge_timestamps(*x):
    # infer which is the list with maximum length
    # as well as individual lengths
    concat = np.concatenate(*x, axis=1)[0]
    lens = np.r_[np.flatnonzero(np.diff(concat) < 0), len(concat)]
    max_len_list = np.r_[lens[0], np.diff(lens)].argmax()
    # define the output matrix 
    A = x[0][max_len_list][0]
    out = np.vstack([A, np.zeros((len(*x)-1, len(A)))])
    others = np.flatnonzero(~np.in1d(np.arange(len(*x)), max_len_list))
    # Update the output matrix with the values of the smaller
    # arrays according to their index. This is of course assuming 
    # all values are contained in the largest
    for ix, i in enumerate(*x[-2:]):
        out[-(ix+1), i[0]-A.min()] = i[1]
    return out

As mentioned this approach does not require a priori knowledge of which is the largest list, i.e. it would also work with:

edited body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
Loading
deleted 2 characters in body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
Loading
deleted 2 characters in body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
Loading
added 1594 characters in body
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
Loading
Source Link
yatu
  • 88.6k
  • 12
  • 93
  • 148
Loading