Python 检查两个给定矩阵是否相同
矩阵是指将数字按行和列排列形成矩形数组。这些数字构成矩阵的元素。
我们需要创建一个Python函数来判断两个给定的矩阵是否相同。换句话说,如果它们对应位置上的所有元素都相同,我们就说两个矩阵是相同的。
相同的矩阵
只有当两个矩阵满足以下要求时,它们才被认为是相等的:
- 每个矩阵的行数和列数应该相等。
- 相应位置上的元素应该在两个矩阵中都存在。
上面的示例中,矩阵A和B是等价的,因为它们具有相同的大小且有相同的对应元素。
步骤
检查两个给定矩阵是否相同的算法如下:
- 创建两个二维数组a和b,并对它们进行初始化。
- 确定数组a的行数和列数,并相应地保存在变量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