python 对角线矩阵_Python | 矩阵的对角线

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 对角线矩阵

你可能感兴趣的:(python,numpy,机器学习,线性代数,人工智能)