How to Perform Matrix Multiplication

88 views

How to Perform Matrix Multiplication

How to Perform Matrix Multiplication

How can you multiply two matrices in Python?

solveurit24@gmail.com Changed status to publish February 20, 2025
0

Use nested loops to perform matrix multiplication.

Code:

# Function to multiply two matrices
def multiply_matrices(mat1, mat2):
    result = [[sum(a * b for a, b in zip(row, col)) for col in zip(*mat2)] for row in mat1]
    return result

# Example matrices
mat1 = [
    [1, 2],
    [3, 4]
]

mat2 = [
    [5, 6],
    [7, 8]
]

# Multiply matrices
result = multiply_matrices(mat1, mat2)

# Print result
for row in result:
    print(row)

solveurit24@gmail.com Changed status to publish February 20, 2025
0