I've looked into the many posts about getting every nth item of an array, and I used the method for slicing even and odd indices. However, I end up with an empty array for the final object. Any suggestions?
floc1 is an array, and I want to subtract every odd element from every even element:
period = abs(floc1[0::2] - floc1[1::2])
This currently gives me an empty array.
EDIT:
I've tried everything suggested in the comments below. The only thing that generated a different error was:
period = [i-j for i, j in zip(floc1[0::2], floc1[1::2])]
This gives:
Phi12 = ((tau)/(period))
ValueError: operands could not be broadcast together with shapes (1,8208) (0,)
In reference to:
Phi12 = ((tau)/(period))
Again, floc1 is definitely not an empty array. I saved it to a text file to confirm.
解决方案
Your example gives an error if floc1 is a list (which people often call an "array"). For a list you can do it this way.
>>> floc1 = [11, 5, 6, 2]
>>> it = iter(floc1)
>>> [x - next(it) for x in it]
[6, 4]
You can also use zip if you prefer see @wenzul's answer
If floc1 is a numpy.array - what you have already works
>>> import numpy as np
>>> floc1 = np.array([11, 5, 6, 2])
>>> abs(floc1[0::2] - floc1[1::2])
array([6, 4])
Perhaps your floc1 is actually an empty array