Sum of diagonal elements in a matrix

后端 未结 11 1942
轻奢々
轻奢々 2021-02-12 11:42

I am trying to find out the sum of the diagonal elements in a matrix. Here, n is the size of the square matrix and a is the matrix. Can someone explain this to me what is happen

11条回答
  •  孤街浪徒
    2021-02-12 12:25

    def sum_diagnol():
        import random
        sum=0
        r1=int(input("row"))
        c1=int(input("col"))
        a=[[random.random()for col in range(c1)]for row in range(r1)]
        print("enter elements")
        for i in range(r1):
            for j in range(c1):
                a[i][j]=int(input("enter elements"))
        r2=int(input("row"))
        c2=int(input("col"))
        b=[[random.random()for col in range(c2)]for row in range(r2)]
        print("enter elements")
        for i in range(r2):
            for j in range(c2):
                b[i][j]=int(input("enter elements"))
        c=[[random.random()for col in range(c2)]for row in range(r1)]
        if(c1==r2):
            for i in range(r1):
                for j in range(c2):
                    c[i][j]=0
                    for k in range(c2):
                        c[i][j]=a[j][k]*b[k][j]
        else:
            print("multiplication not possible")
        for i in range(r1):
            for j in range(c2):
                print(c[i][j],end=" ")
            print()
    sum_diagnol()
    

提交回复
热议问题