Simple question here:
I\'m trying to get an array that alternates values (1, -1, 1, -1.....) for a given length. np.repeat just gives me (1, 1, 1, 1,-1, -1,-1, -1
If you want a memory efficient solution, try this:
def alternator(n): for i in xrange(n): if i % 2 == 0: yield 1 else: yield -1
Then you can iterate over the answers like so:
for i in alternator(n): # do something with i