在 Numpy 中创建上三角为零的下三角矩阵
要创建一个上三角为零的下三角矩阵,可以使用 Python Numpy 中的 **numpy.tri()** 方法。第一个参数是数组的行数,第二个参数是数组的列数。
tri() 函数返回一个数组,其下三角部分填充为 1,其他部分填充为 0;换句话说,当 j <= i + k 时,T[i,j] == 1,否则为 0。
步骤
首先,导入所需的库 -
import numpy as np
创建上三角为零的下三角矩阵,使用 numpy.tri() -
arr = np.tri(4, 4)
显示数组 -
print("Array...
",arr)获取数据类型 -
print("
Array datatype...
",arr.dtype)
获取数组的维度:-
print("
Array Dimensions...
",arr.ndim)获取数组的形状 -
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数 -
print("
Elements in the Array...
",arr.size)示例
import numpy as np
# To create an array with zero above the main diagonal forming a lower triangular matrix, use the numpy.tri() method in Python Numpy
# The 1st parameter is the number of rows in the array
# The 2nd parameter is the number of columns in the array
arr = np.tri(4, 4)
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)输出
Array... [[1. 0. 0. 0.] [1. 1. 0. 0.] [1. 1. 1. 0.] [1. 1. 1. 1.]] Array datatype... float64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP