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
广告