我是Python的新手,我有一个嵌套的for循环.由于for循环需要一段时间才能运行,我正试图找到一种方法来对这段代码进行矢量化,以便它可以更快地运行.
在这种情况下,coord是一个三维数组,其中coord [x,0,0]和coord [x,0,1]是整数,coord [x,0,2]是0或1. H是SciPy稀疏矩阵和x_dist,y_dist,z_dist和a都是浮点数.
# x_dist, y_dist, and z_dist are floats
# coord is a num x 1 x 3 numpy array where num can go into the hundreds of thousands
num = coord.shape[0]
H = sparse.lil_matrix((num, num))
for i in xrange(num):
for j in xrange(num):
if (np.absolute(coord[i, 0, 0] - coord[j, 0, 0]) <= 2 and
(np.absolute(coord[i, 0, 1] - coord[j, 0, 1]) <= 1)):
x = ((coord[i, 0, 0] * x_dist + coord[i, 0, 2] * …Run Code Online (Sandbox Code Playgroud)