python 下三角矩阵_Python | 矩阵的上三角

python 下三角矩阵

A matrix can be seen in different ways and one of them is the upper triangular matrix part. Some problems in linear algebra are concerned with the upper triangular part of the matrix.

可以用不同的方式看到矩阵,其中之一是上三角矩阵部分。 线性代数中的一些问题与矩阵的上三角部分有关。

For this purpose, we have a predefined function numpy.triu(a) in the NumPy library package which automatically stores the upper triangular elements in a separate matrix. In this article, we are going to print the upper triangular elements of a matrix using inbuilt function numpy.triu(a).

为此,我们在NumPy库包中有一个预定义的函数numpy.triu(a) ,该函数自动将上三角元素存储在单独的矩阵中。 在本文中,我们将使用内置函数numpy.triu(a)打印矩阵的上三角元素

Python代码查找矩阵的上三角 (Python code to find upper triangle of a matrix)

# Linear Algebra Learning Sequence
# Upper Triangle of matrix

import numpy as np

print('Upper Triangle of an 3x3 identity matrix : ', np.triu(np.eye(3)))

a = np.arange(9).reshape((3,3))

print('\n\nMatrix a :\n', a)
print('Upper Triangle of Matrix a :\n', np.triu(a))

b = np.triu(np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('\n\nMatrix b :\n', np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('Upper Triangle of Matrix b : ', b)

Output:

输出:

Upper Triangle of an 3x3 identity matrix :  [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]


Matrix a :
 [[0 1 2]
 [3 4 5]
 [6 7 8]]
Upper Triangle of Matrix a :
 [[0 1 2]
 [0 4 5]
 [0 0 8]]


Matrix b :
 [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
Upper Triangle of Matrix b :  [[1 2 3]
 [0 5 6]
 [0 0 9]
 [0 0 0]]


翻译自: https://www.includehelp.com/python/upper-triangle-of-a-matrix.aspx

python 下三角矩阵

你可能感兴趣的:(python,numpy,机器学习,深度学习,人工智能)