I am trying to flip a binary array one at a time.
import numpy as np
k = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1])
for example my out put should be like this;
[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 1st output
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 2nd output
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 3rd output
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 4th output
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 5th output
In the first output, I want to flip only the first element of the array (other elements do not change), in the second output, the second element should change (the 1st and the remaining elements should not change).. etc. Could anyone tell me how can flip one at a time? Thank you
-
1What have you tried so far?IanQ– IanQ2018年11月26日 20:06:04 +00:00Commented Nov 26, 2018 at 20:06
2 Answers 2
What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.
Setup
arr = np.tile(k, 5).reshape(-1, k.shape[0])
array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])
Using numpy.diag_indices:
x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]
array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])
1 Comment
You can use a generator to save memory and even time on big arrays :
k=np.array([0, 0, 1])
def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k
for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]
k is reset at the end of the loop.