1

the following code generates a kind of 'skyline' profile where the initial values are 0's and the profile is made with 1's. I implemented using a for loop and I wonder if there is a more efficient way to obtain the same result.

#https://app.codility.com/programmers/task/max_square_on_matrix/
import numpy as np
np.random.seed(2)
A = np.zeros((5,5), dtype=int)
X = np.random.randint(low=1, high=5, size = 5)

print('A ')
print(A)
print('----------------------')
print('X ')
print(X)
#
for j in range(len(A)):
        A[5-X[j]:5,j] = 1
print('----------------------')
print('A modified ')
print(A)
2
  • 2
    If you were simply setting one value A[5-X, np.arange(5)] =1. But with slice to the end, that approach doesn't work. We need to generate a mask array with something like X[:,None]<np.arange(5). But details need to worked out in an answer. Commented May 26, 2021 at 18:42
  • 1
    Regarding the input size, parallelism will not help. The input should be quite big for parallelism to be useful. Commented May 26, 2021 at 20:13

1 Answer 1

1

Unless you want to use matrix A for some computation which result in numbers other than 1 or 0, you can simply treat 1 and 0 like True and False:

N = 5
X = np.random.randint(low=1, high=N, size = (N,1) )
A = (X >= range(N,0,-1)).T

EDIT:

Oooop, I responded too quickly. Turns out my method is not "more efficient" than the for loop for N=5.

Also, the following works slightly better than my original answer, especially for large N:

a = np.array(range(N,0,-1),ndmin=2).T
X = np.random.randint(low=1, high=N, size=N)
A = X>=a

On my computer, this is a tiny bit slower than the for loop when N=5. However, when N=100, it is 2 times faster than doing the for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, if you really need the A.dtype == int instead of bool, then do A = np.empty((N,N), dtype=int) and A[:] = X>=a.

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.