pro
pro

Reputation: 113

subtracting element of an array from next elements

I have a numpy array i want to subtract the previous number from the next number after fixing the first number and want to replace last number with zero

a=np.array([10,20,22,44])

expected output

np.array([10,10,2,0])

I tried with np.diff function but it misses the first number.Hope experts will suggest better solution.

Upvotes: 1

Views: 60

Answers (1)

mozway
mozway

Reputation: 260390

You want a custom output, so use a custom concatenation:

out = np.r_[a[0], np.diff(a[:-1]), 0]

Output: array([10, 10, 2, 0])

Upvotes: 1

Related Questions