Reputation: 143
By their timestamp, I want to combine 2 arrays with shapes (3, 6) and (3, 8), respectively.
here is the original array:
array([[ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[ 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6],
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]])
i have extracted the values with ones:
res = np.where(a[0] == 1)
ones = a[:,res].squeeze()
zeros = np.delete(a,res,axis=1)
zeros
(3, 8)
array([[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 3, 3, 3, 3, 5, 5, 5, 5],
[ 1, 2, 3, 4, 8, 9, 10, 11]])
ones
(3, 6)
array([[ 1, 1, 1, 1, 1, 1],
[ 4, 4, 4, 6, 6, 6],
[ 5, 6, 7, 12, 13, 14]])
Now i want to combine them again by their last row, which is the timestamp.
Is there any numpy solution to that?
Expected result:
array([[ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[ 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6],
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]])
Upvotes: 0
Views: 77
Reputation: 143
By using the ideas from both users Thomas and Epsi95, this solved my problem so far. Yet, I'm still open to more performant solutions.
m =np.hstack((zeros, ones))
a = m[:, m[2, :].argsort()]
Upvotes: 2
Reputation: 7507
Here is a way (probably not the most compact however):
m = np.hstack((zeros, ones))
m2 = list(zip(m[2], m[0], m[1]))
m2.sort()
np.roll(m2, -1, axis=-1).T
Upvotes: 1