简体   繁体   中英

How to subtract each integer in an array from the previous integer and find the summation

I have a numpy array that has the size (122,) and is made up of integers

I want to use the following formula:

在此处输入图片说明

I tried doing

value=sum((I[:-1]-I[1:])**2))

This line itself does not create an error, but whenever I try to use this value in the bigger formula it gives an invalid syntax error.

I want to use this value to plug into a bigger formula

在此处输入图片说明

I would like an answer of how to plug my array into this formula For instance, the code for the second term in the bigger formula, I have written as follows:

calc = np.linalg.norm((RR_intervals[:-1])-(RR_intervals[1:]))                                                                     

p=(n-1)*sqrt(2)
o=(1/p)*calc
t=o**2

If I try and combine this with the first term I get an invalid syntax error

Full interpreter message: runfile('/home/user_1/p01.py', wdir='/home/user_1') Traceback (most recent call last):

File "/home/user_1/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3291, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in runfile('/home/user_1/p01.py', wdir='/home/user_1')

File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/home/user_1/p01.py", line 91 dispersion = (sqrt(mt)) ^ SyntaxError: invalid syntax

You can do using list :

a = list(numpy_array)
sum([(i-j)**2 for i,j in zip(a,a[1:])])

这应该这样做:

result = sum((ar[i] - ar[i + 1])**2 for i in range(len(ar) - 1))

Using numpy:

(((a-np.roll(a,1))**2)[1:]).sum()

Note: The [1:] is necessary because roll reinserts the element at the beginning of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM