(Python) How to get diagonal(A*B) without having to perform A*B?

前端 未结 2 1754
灰色年华
灰色年华 2021-01-04 19:59

Let\'s say we have two matrices A and B and let matrix C be A*B (matrix multiplication not element-wise). We wish to get

2条回答
  •  独厮守ぢ
    2021-01-04 20:04

    def diag(A,B):
        diags = []
        for x in range(len(A)):
            diags.append(A[x][x] * B[x][x])
        return diags
    

    I believe the above code is that you're looking for.

提交回复
热议问题