-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss2_2.py
More file actions
52 lines (39 loc) · 1.08 KB
/
Ass2_2.py
File metadata and controls
52 lines (39 loc) · 1.08 KB
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
44
45
46
47
48
49
50
51
52
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
matrixAddition = [[0, 0, 0],[0,0,0],[0,0,0]]
matrixSubtraction = [[0, 0, 0],[0,0,0],[0,0,0]]
# iterate through rows
print("Matrix addition")
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
matrixAddition[i][j] = X[i][j] + Y[i][j]
for r in matrixAddition:
print(r)
print()
print("Matrix subtraction")
for i in range(len(X)):
for j in range(len(X[0])):
matrixSubtraction[i][j] = X[i][j] - Y[i][j]
for r in matrixSubtraction:
print(r)
print()
matrix1 = [[12,7,3], [4 ,5,6], [7 ,8,9]]
# 3x4 matrix
matrix2 = [[5,8,1,2], [6,7,3,0],[4,5,9,1]]
# result is 3x4
matrixMutiplication = [[0,0,0,0],[0,0,0,0],[0,0,0,0]]
# iterate through rows of X
print("Matrix multiplication")
for i in range(len(matrix1)):
# iterate through columns of Y
for j in range(len(matrix2[0])):
# iterate through rows of Y
for k in range(len(matrix2)):
matrixMutiplication[i][j] += matrix1[i][k] * matrix2[k][j]
for r in matrixMutiplication:
print(r)