如何逐行遍历numpy矩阵?

时间:2019-03-27 17:16:06

标签: python arrays numpy

如果我有一个numpy数组

[[ 0 -2 -4 -6]
 [-2 -3 -5 -7]
 [-4  0 -2 -4]
 [-6 -2 -3 -5]]

我如何访问这个称为Mymatrix的numpy数组的每个元素,按行,这样mymatrix[x][y]=the element that is traversed,例如,我将得到0,-2,-4,-6,-2,-3,-5,-7,-4,0,-2,-4,-6,-2,依此类推关于数组是否遍历double for循环

编辑:

当我从下面遵循double for循环方法时,这就是我得到的

for row in range(scorematrix.shape[0]):
    for col in range(scorematrix.shape[1]):
        final=[]
        if row==0 and col==0:
            scorematrix[row][col]=0
            continue
        elif row==0:
            scorematrix[row][col]=col+(col*-3)
            continue
        elif col==0:
            scorematrix[row][col]=row+(row*-3)
            continue
        elif seq1[row]=="A" and seq2[col]=="A":
            result=4
        elif seq1[row]=="C" and seq2[col]=="C":
            result=3
        elif seq1[row]=="G" and seq2[col]=="G":
            result=2
        elif seq1[row]=="T" and seq2[col]=="T":
            result=1
        elif seq1[row]!=seq2[col]:
            result=-3
        print(scorematrix)
        firstelement=result+scorematrix[row-1,col-1]
        final.append(firstelement)
        secondelement=scorematrix[row-1,col]-2
        final.append(secondelement)
        thirdelement=scorematrix[row,col-1]-2
        final.append(thirdelement)
        themax=max(final)
        index=[k for k, j in enumerate(final) if j == themax]
        listofc.append(index)

        scorematrix[row][col]=themax

我的问题所必需的主要位是前两行,最后一行,其余只是一些计算。 我要输出的矩阵是[[ 0 -2 -4 -6] [-2 -3 -5 -7] [-4 0 -2 -4] [-6 -2 -3 -5]]

但是我得到的矩阵是

[[ 0 -2 -4 -6] [-2 -3 0 -2] [-4 -5 -2 -3] [-6 -7 -4 -5]] 如果您忽略0,-2,-4,-6中的行和列,则可以看到应该作为第一行的内容已作为第一列输入,依此类推。

我该如何纠正?

0 个答案:

没有答案
相关问题