简体   繁体   中英

how to exploit parallelism in a numpy matrix operation

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM