Knowing a final number (for example 5), I would like to create an array containing the following sequence:
[0,1,1,2,2,3,3,4,4,5]
Meaning that the list should contain all numbers repeated twice, except for the first and last.
Here is the code I use to achieve this :
<code>
import numpy as np
# final number
last = 35
# first sequence
sa = np.arange(0,last,1)
# second sequence (shifted by 1 unit)
sb = np.arange (1,last+1,1)
# concatenation and flattening
sequence = np.stack((sa, sb), axis=1).ravel()
# view the result
print(sequence)
</code>
<code>
import numpy as np
# final number
last = 35
# first sequence
sa = np.arange(0,last,1)
# second sequence (shifted by 1 unit)
sb = np.arange (1,last+1,1)
# concatenation and flattening
sequence = np.stack((sa, sb), axis=1).ravel()
# view the result
print(sequence)
</code>
import numpy as np
# final number
last = 35
# first sequence
sa = np.arange(0,last,1)
# second sequence (shifted by 1 unit)
sb = np.arange (1,last+1,1)
# concatenation and flattening
sequence = np.stack((sa, sb), axis=1).ravel()
# view the result
print(sequence)
Do you think there would be a more direct and/or effective way to achieve the same result?