Matrix multiplication is an operation in which we multiply two or more matrices. A matrix consists of m rows and n columns. Each row from matrix1 is multiplied by each column in matrix2.
Program for matrix multiplication in Python
mat1 = [[14, 5, 8],
[3, 7, 9],
[8, 9, 7]]
mat2 = [[8, 6, 4, 5],
[0, 6, 3, 9],
[3, 12, 7, 9]]
op = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(len(mat1)):
for j in range(len(mat2[0])):
for k in range(len(mat2)):
op[i][j] += mat1[i][k] * mat2[k][j]
for result in op:
print(result)
Output
[136, 210, 127, 187]
[51, 168, 96, 159]
[85, 186, 108, 184]
We have created two matrices in this program, mat1, and mat2, with some elements. Matrix mat1 consists of 3 rows and 3 columns. And matrix mat2 consists of 3 rows and 4 columns. Hence the output matrix is formed for 3 rows and 4 columns.
We iterate over the length of the matrix mat1. Then the second for loop will be for several columns in matrix mat2. Finally, in the third for loop, it will iterate for a length of the matrix mat2. Inside these for loops, we will perform matrix multiplication by multiplying the element present in the i and k of the matrix mat1 and the k and j of the matrix mat2.
The output is printed as rows. This program can be used for multiplying 3 X 3 matrix with 3 X 4 matrix.
Matrix multiplication using list comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
mat1 = [[14, 5, 8],
[3, 7, 9],
[8, 9, 7]]
mat2 = [[8, 6, 4, 5],
[0, 6, 3, 9],
[3, 12, 7, 9]]
op = [[sum(i * j for i, j in zip(a, b))
for b in zip(*mat2)]
for a in mat1]
for result in op:
print(result)
Output
[136, 210, 127, 187]
[51, 168, 96, 159]
[85, 186, 108, 184]
In this example, we used list comprehension to calculate the matrix multiplication. This is similar to the previous approach. Instead of a nested loop, we used list comprehension. This example also has three loops for the operations.
Matrix multiplication using numpy dot() in Python
To perform matrix multiplication in Python, use the np.dot() function. The np.dot() is the numpy library function that returns the dot product of two arrays. Specifically, If both a and b are 1D arrays, it is the inner product of vectors.
import numpy as np
mat1 = [[14, 5, 8],
[3, 7, 9],
[8, 9, 7]]
mat2 = [[8, 6, 4, 5],
[0, 6, 3, 9],
[3, 12, 7, 9]]
op = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
op = np.dot(mat1, mat2)
for result in op:
print(result)
In this program, we used numpy.dot() function to perform matrix multiplication. The dot(.) function multiplies the two matrixes data automatically.
That’s it for this tutorial.
See also
How to Save Array to File in Python
How to use for loop for multiple variables in Python
How to read binary files in Python

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has experience with various programming languages and technologies, including PHP, Python, and JavaScript. He is comfortable working in front-end and back-end development.