Python program to multiply two matrices

Write a Python program to multiply two matrices. 

Matrix multiplication is performed by taking the dot product of rows of the first matrix and columns of the second matrix. The number of columns in the first matrix must equal the number of rows in the second matrix for the multiplication to be valid.

Input-Output Examples

plaintext
Example 1:
Input:
Matrix A:
[[1, 2], 
 [3, 4], 
 [5, 6]]
Matrix B:
[[7, 8], 
 [9, 10]]
Output:
The resultant matrix after multiplication is:
[[25, 28], 
 [57, 64], 
 [89, 100]]

Example 2:
Input:
Matrix A:
[[1, 0], 
 [0, 1]]
Matrix B:
[[5, 6], 
 [7, 8]]
Output:
The resultant matrix after multiplication is:
[[5, 6], 
 [7, 8]]

Algorithm to multiply two matrices

  1. Start
  2. Define the matrices: Initialize two matrices of appropriate dimensions where the number of columns in the first matrix matches the number of rows in the second matrix.
  3. Initialize a result matrix: Create an empty matrix with the appropriate dimensions for the resultant matrix.
  4. Loop through the matrices: Use nested loops to iterate over the rows of the first matrix and the columns of the second matrix.
  5. Multiply and sum elements: For each element in the resultant matrix, compute the sum of the product of corresponding elements from the row of the first matrix and the column of the second matrix.
  6. Store the result: Store the result in the appropriate position in the resultant matrix.
  7. Display the result: Print the resultant matrix after multiplication.
  8. End

Python Program to multiply two matrices

python
# Python program to multiply two matrices

# Define matrix A and matrix B
matrix_a = [
    [1, 2],
    [3, 4],
    [5, 6]
]

matrix_b = [
    [7, 8],
    [9, 10]
]

# Initialize an empty result matrix with the appropriate dimensions
# Result matrix will have rows of matrix_a and columns of matrix_b
result = [[0, 0], 
          [0, 0], 
          [0, 0]]

# Loop through the rows of matrix_a and columns of matrix_b to multiply them
for i in range(len(matrix_a)):  # Loop through rows of matrix_a
    for j in range(len(matrix_b[0])):  # Loop through columns of matrix_b
        for k in range(len(matrix_b)):  # Loop through rows of matrix_b
            result[i][j] += matrix_a[i][k] * matrix_b[k][j]

# Display the result matrix
print("The resultant matrix after multiplication is:")
for row in result:
    print(row)

Code Explanation

  1. Defining the matrices:
    The matrices matrix_a and matrix_b are predefined as 2D lists in Python. The number of columns in matrix_a matches the number of rows in matrix_b, which is a requirement for matrix multiplication.
  2. Initializing the result matrix:
    A result matrix result is initialized with all zeroes. The number of rows in the result matrix is equal to the number of rows in matrix_a, and the number of columns in the result matrix is equal to the number of columns in matrix_b.
  3. Multiplying the matrices:
    The program uses three nested loops:
    • The outer loop iterates over the rows of matrix_a.
    • The middle loop iterates over the columns of matrix_b.
    • The inner loop performs the dot product by multiplying elements from the rows of matrix_a with the corresponding elements from the columns of matrix_b, and summing them up.
  4. Displaying the output:
    After the matrices are multiplied, the program uses a for loop to print each row of the result matrix.

Python

6490

125

Related Articles