使用 Python 交换矩阵的对角线
在这篇文章中,我们将学习一个 Python 程序来交换矩阵的对角线。
假设我们已经得到了一个NxN的输入矩阵。我们现在将使用以下方法交换输入矩阵的对角线。
使用的方法
以下是完成此任务的各种方法 -
使用嵌套 For 循环和临时变量
使用','交换运算符
算法(步骤)
以下是执行所需任务的算法/步骤 -
创建一个变量来存储矩阵的行数输入
创建一个函数printGivenMatrix()来打印给定的矩阵。
在 printGivenMatrix() 函数内部,使用 For 循环遍历给定矩阵的行。
使用另一个嵌套 For 循环遍历当前行的列。
打印当前行和列处对应的矩阵元素。
创建一个函数swapMatDiagonals(),通过接受输入矩阵作为参数来交换输入矩阵的对角线。
使用for 循环遍历矩阵的行。
使用if 条件语句检查行索引是否不等于矩阵行数的一半。
如果条件为true,则使用临时变量交换对角线的元素。
创建一个变量来存储输入矩阵。
通过将输入矩阵作为参数传递给上面定义的swapMatDiagonals()函数来交换矩阵的对角线。
再次通过调用printGivenMatrix() 方法打印输入矩阵。
方法 1:使用嵌套 For 循环和临时变量
示例
以下程序使用嵌套 for 循环和临时变量交换矩阵的对角线 -
# creating a function to print the given matrix
def printGivenMatrix(inputMatrix):
# Traversing in the rows of the input matrix
for p in range(rows):
# Traversing in the columns corresponding to the current row
# of the input matrix
for q in range(rows):
# printing the element at the current row and column
print(inputMatrix[p][q], end=" ")
# Printing a new line to separate the rows
print()
# creating a function to interchange the diagonals
# of matrix by accepting input matrix as an argument
def swapMatDiagonals(inputMatrix):
# traversing through the rows of a matrix
for p in range(rows):
# checking whether the row index is not half of the rows of a matrix
if (p != rows / 2):
# swapping elements of diagonals using a temporary variable
tempVariable = inputMatrix[p][p]
inputMatrix[p][p] = inputMatrix[p][rows - p - 1]
inputMatrix[p][rows - p - 1] = tempVariable
# input no of rows of a matrix
rows = 3
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[6, 10, 8],
[7, 2, 4]]
print("The Given Matrix is:")
printGivenMatrix(inputMatrix)
print("Interchanging Diagonals of an input matrix:")
#calling the above swapMatDiagonals() function bypassing the input matrix to it
swapMatDiagonals(inputMatrix)
# Printing the matrix again after interchanging the diagonals of it
printGivenMatrix(inputMatrix)
输出
执行上述程序后,将生成以下输出 -
The Given Matrix is: 5 1 3 6 10 8 7 2 4 Interchanging Diagonals of an input matrix: 3 1 5 6 10 8 4 2 7
时间复杂度 - O(N)
其中 N 表示行数或列数,因为我们只使用一个循环来交换指定矩阵的对角线。
辅助空间 - O(1)。因为我们没有使用任何额外的空间。
方法 2:使用','交换运算符
示例
以下程序使用','(交换)运算符交换矩阵的对角线 -
# creating a function to print the given matrix
def printGivenMatrix(inputMatrix):
# Traversing in the rows of the input matrix
for p in range(rows):
# Traversing in the columns corresponding to the current row
for q in range(rows):
# printing the element at the current row and column
print(inputMatrix[p][q], end=" ")
# Printing a new line to separate the rows
print()
# creating a function to interchange the diagonals of the matrix
def swapMatDiagonals(inputMatrix):
# traversing through the rows of a matrix
for p in range(rows):
# checking whether the row index is not half of the rows of a matrix
if (p != rows / 2):
# swapping elements of diagonals using ','(swapping operator)
inputMatrix[p][p], inputMatrix[p][rows - p - 1] = inputMatrix[p][rows - p - 1], inputMatrix[p][p]
# input no of rows of a matrix
rows = 3
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[6, 10, 8],
[7, 2, 4]]
print("The Given Matrix is:")
printGivenMatrix(inputMatrix)
print("Interchanging Diagonals of an input matrix:")
# calling the above swapMatDiagonals() function
swapMatDiagonals(inputMatrix)
# Printing the matrix again after interchanging the diagonals of it
printGivenMatrix(inputMatrix)
输出
执行上述程序后,将生成以下输出 -
The Given Matrix is: 5 1 3 6 10 8 7 2 4 Interchanging Diagonals of an input matrix: 3 1 5 6 10 8 4 2 7
时间复杂度 - O(N)
辅助空间 - O(1)。因为我们没有使用任何额外的空间。
结论
在这篇文章中,我们学习了两种不同的方法来交换矩阵的对角线元素。此外,我们学习了如何使用交换运算符 (',')(临时变量)在不占用额外空间的情况下交换两个变量。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP