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).reshape((3,3))
print(' Matrix a : ', a)
print('Diagonal of Matrix a : ', np.diag(a))
Output: 輸出: Diagonal of an 3x3 identity matrix : [1. 1. 1.]
Matrix a : [[0 1 2]
[3 4 5]
[6 7 8]]
Diagonal of Matrix a : [0 4 8]
翻譯自: https://www.includehelp.com/python/diagonal-of-a-matrix.aspx
python 對角線矩陣