使用 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