Python程式檢查兩個給定矩陣是否相同
矩陣指的是一系列按行和列排列的數字,形成一個矩形陣列。這些數字構成了矩陣的條目或元素。
我們需要建立一個Python函式來確定兩個給定的矩陣是否相同。換句話說,如果兩個矩陣中各自位置的所有元素都相同,我們則認為這兩個矩陣是相同的。
相同矩陣
只有當兩個矩陣滿足以下條件時,才被認為是相等的:
- 每個矩陣的行數和列數應相等。
- 兩個矩陣中應該存在相同的對應元素。
上面示例中的矩陣A和B是等價的,因為它們具有相同的大小,並且具有相同的對應元素。
演算法
以下是檢查兩個給定矩陣是否相同的演算法:
- 建立兩個二維陣列a和b,然後初始化它們。
- 確定陣列的行數和列數,並將結果分別儲存在變數row1和col1中。
- 確定陣列b的行數和列數,並將結果分別儲存在變數row2和col2中。
- 將變數flag的初始值設定為true。
- 檢查陣列的大小是否相等。如果陣列大小不相等,則顯示“這兩個矩陣不相等”。
- 如果大小相等,則應遍歷兩個陣列並比較每個元素。
- 如果任何相關元素不相等,則將標誌設定為false並結束迴圈。
示例
使用迴圈
以下是一個檢查兩個矩陣是否相等的示例:
#Initializing the matrix A A = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; #Initializing the matrix B B = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; flag = True; #Calculating the no of rows and columns present in the first matrix r1 = len(A); c1 = len(A[0]); #Calculating the no. of rows and columns present in the second matrix r2 = len(B); c2 = len(B[0]); #Checking whether the dimensions of both the matrices are equal if(r1 != r2 or c1 != c2): print("The two matrices are not equal"); else: for i in range(0, r1): for j in range(0, c1): if(A[i][j] != B[i][j]): flag = False; break; if(flag): print("The two matrices are equal"); else: print("The two matrices are not equal");
輸出
以下是上述程式碼的輸出:
The two matrices are equal The two matrices are equal The two matrices are equal
示例
比較兩個矩陣中每行的元素。如果相同,則轉到下一行。如果不相同,則列印“這兩個矩陣不相同”並中斷迴圈。如果迴圈沒有中斷,則矩陣相同,如下例所示:
#Initializing the matrix A A = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; #Initializing the matrix B B = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; X=0 for i in range(len(A)): for j in range(len(B)): if A[i][j]!=B[i][j]: X=1 break if X==1: break if X==0: print("The two matrices are Identical") else: print("The two matrices are not Identical")
輸出
以下是上述程式碼的輸出:
The two matrices are Identical
示例
建立兩個矩陣。然後,遍歷第一個和第二個矩陣中的每個元素,並在它們之間進行比較。如果兩者都相同,則兩個矩陣都相同,如下例所示:
V=4 # This function returns 1 if a[][] and b[][] are identical otherwise it returns 0 def identical(a,b): for i in range(s): for j in range(s): if (a[i][j] != b[i][j]): return 0 return 1 # the driver code a=[] s=int(input("Enter S for S x S matrix : ")) #using the list to store 2D array and getting the user input and storing it in the list print("Enter the elements ::>") for i in range(s): #temporarily list for storing the row row = [] for j in range(s): # adding the input to the row list row.append(int(input())) # adding the row to the list a.append(row) print(a) # [[6, 3, 8], [5, 0, 2], [7, 1, 4]] # Displaying the 2D array print("Displaying the array In the Matrix Form") for i in range(s): for j in range(s): print(a[i][j], end=" ") print() b=[] s=int(input("Enter S for S x S matrix : ")) #using the list to store 2D array and getting the user input and storing it in the list print("Enter the element ::>") for i in range(s): #temporarily list for storing the row row = [] for j in range(s): #add the input to row list row.append(int(input())) # adding the row to the list b.append(row) print(b) # [[6, 3, 8], [5, 0, 2], [7, 1, 4]] # Displaying the 2D array print("Display Array In Matrix Form") for i in range(s): for j in range(s): print(b[i][j], end=" ") print() if (identical(a, b)==1): print("The two matrices are Identical") else: print("The two matrices are not Identical")
輸出
以下是上述程式碼的輸出:
Enter S for S x S matrix : 3 Enter the elements ::> 6 3 8 5 0 2 7 1 4 [[6, 3, 8], [5, 0, 2], [7, 1, 4]] Displaying the array In the Matrix Form 6 3 8 5 0 2 7 1 4 Enter S for S x S matrix : 3 Enter the element ::> 6 3 8 5 0 2 7 1 4 [[6, 3, 8], [5, 0, 2], [7, 1, 4]] Display Array In Matrix Form 17 6 3 8 5 0 2 7 1 4 The two matrices are Identical
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP