矩阵扫描中的三重循环

时间:2017-06-26 09:11:59

标签: python numpy for-loop matrix

正如在标题中所说,我实际上在我的python程序中使用三重for循环来处理一个名为A的numpy矩阵,正如我们所预料的那样,它非常慢。 我有一个功能,我们打电话给" return_bool"它返回参数h的函数中的布尔值,i和j返回矩阵元素的索引。我想将这个函数用于h的几个值来制作一个"梯度矩阵"。 这是我的代码:

A = np.zero(iindex,jindex)
for h in hvalue:
    for in iindex:
        for j in jindex:
             if (return_bool(h,i,j) : A[i][j] =+ 1

有没有提高矩阵扫描速度的提示?

这是我" return_bool"的代码。 function(所有值都是float或者带有x和y点的点(x,y)):

def inclu_geo(coord1,coord2,y1,y2 , hh , hh2,y1droite,y2droite, hhdroite , hh2droite,intersec1,intersec2,rho):
    global y
    global yprime
    global largeur_pale
    #equation des droites 
    equdroite1 = eqdroite(y1,y2,hh,hh2)
    equdroite2 = eqdroite(y1droite,y2droite,hhdroite,hh2droite)
    if   0< rho < 90 :
        if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
            if inclu(intersec1[0],intersec1[1]):
                b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((intersec1[0]<=coord1<=yprime)and(hh2<=coord2<=intersec1[1]))or((y1<=coord1<=intersec1[0])and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
            if inclu(intersec2[0],intersec2[1]):
                b = ((y<=coord1<=intersec2[0])and(hh2droite<=coord2<=intersec2[1]))or((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((intersec2[0]<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
        else:
            if (hh != 0) and inclu(y1,hh):
                b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((y1<=coord1<=y2)and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
            elif (hhdroite != 0) and inclu(y1droite,hhdroite):
                b = ((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((y2droite<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
            elif (hhdroite != 0) or (hh != 0):
                b = True
            else:
                b = False
    else:
        if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
                if inclu(intersec1[0],intersec1[1]):
                    b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((intersec1[0]<=coord1<=yprime)and(intersec1[1]<=coord2<=hh2))or((y1<=coord1<=intersec1[0])and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
                if inclu(intersec2[0],intersec2[1]):
                    b = ((y<=coord1<=intersec2[0])and(intersec2[1]<=coord2<=hh2droite))or((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((intersec2[0]<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
        else:
                if (hh != largeur_pale) and inclu(y1,hh):
                    b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((y1<=coord1<=y2)and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
                elif (hhdroite != largeur_pale) and inclu(y1droite,hhdroite):
                    b = ((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((y2droite<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
                elif (hhdroite != largeur_pale) or (hh != largeur_pale):
                    b = True
                else:
                    b = False
    return b

1 个答案:

答案 0 :(得分:0)

我重写了我的return_bool(h,i,j)来逐行处理矩阵(return_bool(h,i)),用这种方法需要2.5秒而不是416秒,所以问题就解决了我猜测。 我用过“&amp;”和“|”使我的逻辑方程适应整行。 感谢大家的帮助。