Multiplication of Two Matrix in Python of Same Dimensions using For loop
ADVERTISEMENTS
Multiplication of two matrix in python of same dimensions using for loop. In this article, you will learn how to make program of multiplication of two matrix in python of same dimensions using for loop.
Matrix Multiplication Formula
{ M1 } x { M2 } = { M3 }
Source Code
# Multiplication of Two Matrix in Python of Same Dimensions using For loop
x, y, m, r, c, i, j, k = [], [], [], 0, 0, 0, 0, 0
# r - Value of the number of rows
# c - Value of the number of columns
print ("Enter the number of rows & columns of the matrix::")
r = int (input ())
c = int (input ())
# x - To store first matrix elements
# y - To store second matrix elements
# m - To store multiplication of matrices
print ("\n---Enter the first matrix's elements---")
for i in range (r):
x.append([])
for j in range (c):
x[i].append(int (input ()))
print ("\n---Enter the second matrix's elements---")
for i in range (r):
y.append([])
for j in range (c):
y[i].append(int (input ()))
print ("\n-----The multiplication of the matrices is-----\n")
# It's the calculation of matrices' multiplications
for i in range (r):
m.append([])
for j in range (c):
m[i].append(0)
for k in range (c):
m[i][j] += x[i][k] * y[k][j]
# This will display matrices's outputs
for i in range (r):
print ("\t")
for j in range (c):
print (m[i][j], end="\t")
print ("\t", end="\t")
Output
Enter the number of rows & columns of the matrix::
4
3
---Enter the first matrix's elements---
32
54
65
45
54
65
45
32
65
45
32
54
---Enter the second matrix's elements---
54
24
76
90
24
76
90
54
76
90
54
24
-----The multiplication of the matrices is-----
12438 5574 11476
13140 5886 12464
11160 5358 10792
10170 4764 9956