189k views
2 votes
Please write code in python to be paying attention to all the

details
Write a function named matProd that takes two matrices (two 2-dimensional lists of floats) and returns their cross products. The function should return "Matrix sizes do not match" if the row and colum

User Basir Alam
by
8.3k points

1 Answer

5 votes

Final answer:

To multiply two matrices and return their cross product in Python, you can write a function that uses nested loops to iterate over the rows and columns of the matrices.

Step-by-step explanation:

To write a function in Python to multiply two matrices and return their cross product, you can use nested loops to iterate over the rows and columns of the matrices. Here is an example of how the code could be implemented:

def matProd(matrix1, matrix2):
if len(matrix1[0]) != len(matrix2):
return 'Matrix sizes do not match'
else:
rows1 = len(matrix1)
cols1 = len(matrix1[0])
cols2 = len(matrix2[0])
result = [[0 for _ in range(cols2)] for _ in range(rows1)]

for i in range(rows1):
for j in range(cols2):
for k in range(cols1):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result

matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]

print(matProd(matrix1, matrix2))