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
- Start
- 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.
- Initialize a result matrix: Create an empty matrix with the appropriate dimensions for the resultant matrix.
- Loop through the matrices: Use nested loops to iterate over the rows of the first matrix and the columns of the second matrix.
- 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.
- Store the result: Store the result in the appropriate position in the resultant matrix.
- Display the result: Print the resultant matrix after multiplication.
- 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
- Defining the matrices:
The matricesmatrix_a
andmatrix_b
are predefined as 2D lists in Python. The number of columns inmatrix_a
matches the number of rows inmatrix_b
, which is a requirement for matrix multiplication. - Initializing the result matrix:
A result matrixresult
is initialized with all zeroes. The number of rows in the result matrix is equal to the number of rows inmatrix_a
, and the number of columns in the result matrix is equal to the number of columns inmatrix_b
. - 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 ofmatrix_b
, and summing them up.
- The outer loop iterates over the rows of
- Displaying the output:
After the matrices are multiplied, the program uses afor
loop to print each row of the result matrix.