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 :
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?
1
Just a mini-demo; but this does the trick. Is it faster? For so short arrays, probably not something you would notice 🙂
a = np.arange(6)
np.array((a,a)).T.ravel()[1:-1]
Inspiration taken from here
What about using arange
on 2*N
, add 1 and take the floor division by 2?
N = 5
out = (np.arange(2*N)+1)//2
Alternatively, with repeat
and excluding the first/last:
out = np.repeat(np.arange(N+1), 2)[1:-1]
Or with broadcasting:
out = (np.arange(N)[:, None]+[0, 1]).ravel()
Output: array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5])
You might use numpy.kron
for get each item repeated twice following way
import numpy as np
arr = np.kron(np.arange(6),[1,1])[1:-1]
print(arr) # [0 1 1 2 2 3 3 4 4 5]
I create array with each item repeated twice, then slice to get rid of 1st and last element