Python 交换矩阵的对角线
在本文中,我们将学习使用Python交换矩阵的对角线的程序。
假设我们已经输入了一个NxN的矩阵。我们将使用以下方法交换输入矩阵的对角线。
使用的方法
以下是实现此任务的各种方法:
- 使用嵌套的for循环和临时变量
-
使用‘,’交换运算符
步骤
下面是执行所需任务的算法/步骤。
- 创建一个变量来存储矩阵的行数
-
创建一个名为printGivenMatrix()的函数,用于打印给定的矩阵
-
在printGivenMatrix()函数内使用for循环遍历给定矩阵的行
-
使用另一个嵌套的for循环遍历当前行的列
-
打印当前行和列的对应矩阵元素
-
创建一个名为swapMatDiagonals()的函数,通过接受输入矩阵作为参数来交换输入矩阵的对角线
-
使用for循环遍历矩阵的行
-
使用if条件语句检查行索引是否不等于矩阵行数的一半
-
如果条件为真,则使用临时变量交换对角线上的元素
-
创建一个变量来存储输入矩阵
-
通过调用上述定义的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)。因为我们没有使用任何额外的空间。
结论
在本文中,我们学习了两种不同的方法来交换矩阵的对角线元素。此外,我们还学习了如何通过使用交换操作符(’,’)(临时变量)来交换两个变量而不占用额外的空间。