Is it possible to combine for loop
and numpy.tile
to create an alternating incremental array?
Asked
Active
Viewed 97 times
-2

Vadim Kotov
- 8,084
- 8
- 48
- 62

Thomas.F
- 7
- 1
-
`tile()` repeats the same values, it looks like you want something else. How about `np.arange()`? – Jussi Nurminen Apr 13 '22 at 08:23
-
`[(-1)**i * (2*i+1) for i in range(10)] `? – Mr. T Apr 13 '22 at 08:25
2 Answers
0
numpy.tile(A, reps)
constructs an array by repeating A the number of times given by reps. Since you want to alternate values, I don't think it is possible (or at least, preferable) to use this function to achieve your goal.
How about using only a for
loop (list comprehension)?
output = [i * (1 if i % 4 == 1 else -1) for i in range(1, 25, 2)]

Xiddoc
- 3,369
- 3
- 11
- 37
0
use resize()
:
In [38]: np.resize([1,-1], 10) # 10 is the length of result array
Out[38]: array([ 1, -1, 1, -1, 1, -1, 1, -1, 1, -1])
for odd length:
In [39]: np.resize([1,-1], 11)
Out[39]: array([ 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1])

rammelmueller
- 1,092
- 1
- 14
- 29

IronicRayquaza
- 1
- 1
- 3