Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy difference between neighboring elements

Tags:

I have algorithm of calculation of the difference between neighboring elements in pure python:

    a = range(1000000) #it's numpy array in my case     prev = a[0]     b = [0, ]     for i in a[1:]:         b.append(i - prev)         prev = i 

Is there any way to rewrite this functions with Numpy?

like image 593
Artem Mezhenin Avatar asked Jun 19 '12 06:06

Artem Mezhenin


1 Answers

There is the diff method:

a = range(5) # python list of numpy array np.diff(a) 

returns

array([1, 1, 1, 1]) 
like image 166
eumiro Avatar answered Sep 28 '22 08:09

eumiro