Issue
I have 2D array
[1, 5],
[2, 7],
[3, 9]
I want to result the array
[1, 5],
[2, 2],
[3, 2]
which is achieved by subtracting only columns and keeping the row as its. (7-5) (9-7)
I tried this below but im not able to get the proper output
import numpy as np
timeline = np.array([[1,5],
[2,7],
[3,9]])
def non_adjacent_diff(row):
not_zero_index = np.where(row != 0)
diff = row[not_zero_index][1:] - row[not_zero_index][:-1]
np.put(row, not_zero_index[0][1:], diff)
return row
np.apply_along_axis(non_adjacent_diff, 1, timeline)
print(timeline)
Solution
You can try this solution:
timeline[1:,-1] = np.diff(timeline[:,-1], axis=0)
Answered By - MSS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.