-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_matrix_multiplication.py
46 lines (36 loc) · 1.17 KB
/
9_matrix_multiplication.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def getMatrix(matrix):
row = int(input('Enter the no. rows:'))
column = int(input('Enter the no. columns:'))
tempList = list()
temp = 0
for i in range(row):
for j in range(column):
temp = int(raw_input('Enter element [{0}][{1}]'.format(i,j)))
tempList.append(temp)
matrix.append(tempList)
tempList = []
def multiplyMatrix(matrix1,matrix2):
resultMatrix = list()
tempList = [];temp =0
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
tempList.append(0)
resultMatrix.append(tempList)
tempList = []
if len(matrix1[0]) == len(matrix2):
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
resultMatrix[i][j] = resultMatrix[i][j] + (matrix1[i][k]*matrix2[k][j])
print('\nResultant Matrix:')
for i in range(len(resultMatrix)):
print(resultMatrix[i])
else:
print 'invalid operation.'
matrix1 = list()
matrix2 = list()
print 'Matrix A: \n'
getMatrix(matrix1)
print 'Matrix B: \n'
getMatrix(matrix2)
multiplyMatrix(matrix1,matrix2)