0

I want to create a new array which basically consists of elements obtained after subtracting the adjacent elements of I.

import numpy as np
I=np.array([0.42741221, 0.32863457, 0.03597327, 0.23270579, 0.24660262,
       0.28189469, 0.30789726, 0.26947605, 0.11872097, 0.0089662 ,
       0.3677314 , 0.00606158, 0.2923718])

Desired output:

np.array([(0.42741221-0.32863457), (0.32863457-0.03597327), (0.03597327-0.23270579),...])
2
  • 1
    Have you had a look at np.diff()? Commented Feb 21, 2022 at 14:39
  • 1
    Please post the code that worked for you below as an answer so that your question can be useful to other users. Commented Feb 21, 2022 at 14:43

1 Answer 1

1

Using np.diff() helps.

import numpy as np
I=np.array([0.42741221, 0.32863457, 0.03597327, 0.23270579, 0.24660262,
       0.28189469, 0.30789726, 0.26947605, 0.11872097, 0.0089662 ,
       0.3677314 , 0.00606158, 0.2923718])
np.diff(I)

This yields the desired output:

array([-0.09877764, -0.2926613 ,  0.19673252,  0.01389683,  0.03529207,
        0.02600257, -0.03842121, -0.15075508, -0.10975477,  0.3587652 ,
       -0.36166982,  0.28631022])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.