0

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

asked Nov 26, 2018 at 20:02
1
  • 1
    What have you tried so far? Commented Nov 26, 2018 at 20:06

2 Answers 2

1

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]])
answered Nov 26, 2018 at 20:11
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @user3483203. Very helpful technique!!
1

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.

answered Nov 26, 2018 at 20:35

1 Comment

Thanks @B. M.. very helpful!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.