1

Consider the following mask:

def maskA(n):
    assert((n % 2) == 0)

    sample_arr = [False, False]
    bool_arr = np.random.choice(sample_arr, size=(n, n))
    # print(bool_arr.shape)

    for i in range(n):
        for j in range(n):
            if (i >= n//2) and (j < n//2):
                bool_arr[i, j] = True
            else:
                bool_arr[i, j] = False

    for i in range(n):
        for j in range(n):
            if (i < n//2) and (j >= n//2):
                bool_arr[i, j] = True or bool_arr[i, j]
            else:
                bool_arr[i, j] = False or bool_arr[i, j]
    return bool_arr

which select elements between clusters (2 x n/2 subnetworks or True elements) in a network.

[[False False False  True  True  True]
 [False False False  True  True  True]
 [False False False  True  True  True]
 [ True  True  True False False False]
 [ True  True  True False False False]
 [ True  True  True False False False]]

is it possible to make it better (shorter, cleaner, faster)?

1
  • I think this should be posted on Code Review Stack Exchange instead, as there does not seem to be any problems being faced that needs solving, rather only suggestions, as to how to modify the code to make it more efficient are needed. Commented Mar 30, 2022 at 10:06

1 Answer 1

1

You can use indexing to assign values instead of using for-loops.

import numpy as np

def maskA(n):
    assert((n % 2) == 0)

    bool_arr = np.full((n, n), True)
    bool_arr[0:int(n/2), 0:int(n/2)] = False
    bool_arr[int(n/2):n, int(n/2):n] = False

    return bool_arr


print(maskA(8))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.