codescracker


python

Python Matrix Multiplication Program



« Previous Program Next Program »


Matrix Multiplication Program in Python

To perform matrix multiplication or to multiply two matrices in python, you have to choose three matrices. Initially, all the element of the third matrix will be zero. Now perform the matrix multiplication and store the multiplication result in the third matrix one by one as shown here in the program given below.

Python Programming Code to Matrix Multiplication

Following python program choose the two matrix and another empty matrix to perform and print the matrix multiplication:

# Python Program - Matrix Multiplication
		
matrix1 = [[1, 2, 3],
	   [4, 5, 6],
	   [7, 8, 9]]
matrix2 = [[10, 11, 12, 13],
	   [14, 15, 16, 17],
	   [18, 19, 20, 21]]
rmatrix = [[0, 0, 0, 0],
	   [0, 0, 0, 0],
	   [0, 0, 0, 0]]
for i in range(len(matrix1)):
    for j in range(len(matrix2[0])):
        for k in range(len(matrix2)):
            rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]	
for r in rmatrix:
    print(r)

Here is the sample run of the above python program to illustrates how to perform matrix multiplication:

matrix multiplication python

Below is the screenshot of python shell, where the same program is written and run:

python matrix multiplication

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test