python 对角线矩阵
Some problems in linear algebra are mainly concerned with diagonal elements of the matrix. For this purpose, we have a predefined function numpy.diag(a) in NumPy library package which automatically stores diagonal elements in an array (a Vector). In this article, we are going to print the diagonal elements of a matrix using inbuilt function numpy.diag(a).
线性代数中的一些问题主要与矩阵的对角线元素有关。 为此,我们在NumPy库包中提供了预定义的函数numpy.diag(a),该函数自动将对角线元素存储在数组(向量)中。 在本文中,我们将使用内置函数numpy.diag(a)打印矩阵的对角线元素。
Python代码查找矩阵的对角线 (Python code to find diagonal of a matrix)
# Linear Algebra Learning Sequence
# Diagonal of matrix
import numpy as np
print('Diagonal of an 3x3 identity matrix : ', np.diag(np.eye(3)))
a = np.arange(9