使用 Python 计算矩阵对角线之和
在本文中,我们将学习一个 Python 程序,以有效地计算矩阵对角线的和。
使用的方法
以下是完成此任务的各种方法 -
使用嵌套 For 循环
仅使用单个循环
在矩阵中,我们有两个对角线 -
主对角线
副对角线
示例
让我们以一个 3x3 矩阵为例,如下所示 -
A00 A01 A02 A10 A11 A12 A20 A21 A22
主对角线条件 - 行列条件为行 = 列。在 3x3 矩阵中,主对角线由A00、A11 和 A22元素构成。
副对角线条件 - 行列条件为行 = 行数 – 列 -1。在 3x3 矩阵中,主对角线由A02、A11 和 A22元素构成。
方法 1:使用嵌套 For 循环
算法(步骤)
以下是执行所需任务的算法/步骤。-
创建一个函数sumOfDiagonals(),通过接受输入矩阵和行数作为参数来打印矩阵对角线的和。
初始化一个变量为 0,以存储主对角线之和。
初始化另一个变量为 0,以存储副对角线之和。
使用for 循环遍历矩阵的行。
使用另一个嵌套 for 循环,遍历当前行的所有列。
使用if 条件语句检查行号是否等于列号(主对角线条件),如果为真,则将矩阵元素值添加到主对角线之和。
同样地,使用if 条件语句检查行号和列号的和是否等于行数(副对角线条件),如果为真,则将矩阵元素值添加到副对角线之和。
打印输入矩阵主对角线元素的最终和。
打印输入矩阵副对角线元素的最终和。
创建一个变量来存储输入矩阵。
通过传递输入矩阵和行数(维度)作为参数,调用上面定义的sumOfDiagonals()函数,以打印对角线的和。
示例
以下程序使用嵌套 for 循环返回输入矩阵的对角线之和 -
# creating a function to print the sum of diagonals
# of a matrix by accepting input matrix, rows as arguments
def sumOfDiagonals(inputMatrix, rows):
# Initializing with 0 to store the principal diagonal sum
principal_diag = 0
# Initializing with 0 to store the secondary diagonal sum
secondary_diag = 0
# Traversing through the rows of a matrix
for p in range(0, rows):
# Traversing through the columns of the current row
for q in range(0, rows):
# Principal diagonal condition
if (p == q):
principal_diag += inputMatrix[p][q]
# Secondary diagonal condition(row -1 because index starts from 0)
if ((p + q) == (rows - 1)):
secondary_diag += inputMatrix[p][q]
# Printing the sum of principal diagonal elements
print("Sum of principal diagonal elements:", principal_diag)
# Printing the sum of secondary diagonal elements
print("Sum of secondary diagonal elements:", secondary_diag)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[9, 6, 8],
[4, 2, 7]]
rows = 3
print("Given Matrix is:")
# traversing through the rows of a matrix
for p in range(rows):
# Traversing through the columns of a current row
for q in range(rows):
# printing the corresponding element at the current row and column of the matrix
print(inputMatrix[p][q], end=" ")
# Printing a new line
print()
# calling sumOfDiagonals() function by passing input matrix
# and no of rows(dimensions) to it
sumOfDiagonals(inputMatrix, rows)
输出
执行上述程序后,将生成以下输出 -
Given Matrix is: 5 1 3 9 6 8 4 2 7 Sum of principal diagonal elements: 18 Sum of secondary diagonal elements: 13
时间复杂度 - O(N*N),因为我们使用了嵌套循环遍历 N*N 次。
辅助空间 - O(1)。因为我们没有使用任何额外的空间。
方法 2:仅使用单个循环
示例
以下程序仅使用一个 for 循环(单个循环)返回输入矩阵的对角线之和 -
# Creating a function to print the sum of diagonals
# of a matrix by accepting input matrix, rows as arguments
def sumOfDiagonals(inputMatrix, rows):
# Initializing with 0 to store the principal diagonal sum
principal_diag = 0
# Initializing with 0 to store the secondary diagonal sum
secondary_diag = 0
# Traversing the rows of a matrix
for p in range(0, rows):
# Adding the principal Diagonal element of the current row
principal_diag += inputMatrix[p][p]
# Adding the secondary Diagonal element of the current row
secondary_diag += inputMatrix[p][rows - p - 1]
# printing the sum of principal diagonal elements
print("Sum of principal diagonal elements:", principal_diag)
# printing the sum of secondary diagonal elements
print("Sum of secondary diagonal elements:", secondary_diag)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[9, 6, 8],
[4, 2, 7]]
rows = 3
print("The Given Matrix is:")
# traversing through the rows of a matrix
for p in range(rows):
# Traversing through the columns of a current row
for q in range(rows):
# printing the corresponding element at the current row and column of the matrix
print(inputMatrix[p][q], end=" ")
# Printing a new line
print()
# calling sumOfDiagonals() function by passing input matrix
# and no of rows(dimensions) to it
sumOfDiagonals(inputMatrix, rows)
输出
执行上述程序后,将生成以下输出 -
Given Matrix is: 5 1 3 9 6 8 4 2 7 Sum of principal diagonal elements: 18 Sum of secondary diagonal elements: 13
时间复杂度 - O(N)。因为我们使用了循环遍历 N 次。
辅助空间 - O(1)。因为我们没有使用任何额外的空间。
结论
在本文中,我们学习了矩阵对角线以及计算矩阵对角线(主对角线和副对角线)之和的两种不同方法。我们学习了一种有效的计算方法,该方法只需要遍历一次矩阵(O(N) 时间复杂度)。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP