Matrix Multiplication in Python
Here you will learn to write a python program to perform Matrix Multiplication or Matrix Multiplication in Python programming.
Python program to perform matrix multiplication
Multiplication of two matrices is a process of multiplying two matrices to produce a single matrix. This can be done by multiplying each element(row) of one matrix by each element(column) of the other matrix.
For example, if matrix A is 2 x 3 and matrix B is 3 x 2, then the result of A * B will be a 2 x 2 matrix.
Program Code of Matrix Multiplication in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #Multiplication of Two 3*3 Matrices in Python # matrix 1 X = [[1,2,3], [4,5,6], [7,8,9]] # matrix 2 Y = [[1,2,3], [4,5,6], [7,8,9]] # result result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows of X for i in range(len(X)): # iterate through columns of Y for j in range(len(Y[0])): # iterate through rows of Y for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r) |
Output
[30, 36, 42]
[66, 81, 96]
[102, 126, 150]
Check out our other Python programming examples