-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37_Matrix.py
More file actions
49 lines (37 loc) · 1010 Bytes
/
Copy path37_Matrix.py
File metadata and controls
49 lines (37 loc) · 1010 Bytes
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
# 1.Addition
print("1.Addition\n")
a = [
[5,8,2],
[2,0,11],
[7,4,9]
]
b = [
[10,8,6],
[2,5,11],
[7,3,9]
]
for i in range(len(a)):
for j in range(len(b[0])):
c = a[i][j] + b[i][j]
print(f"{c:3}",end=" ")
print()
# 2. Multiplication
print("\n2. Multiplication\n")
a = [[i+j*3+1 for i in range(3)] for j in range(3)]
b = [[i+j*3+1 for i in range(3)] for j in range(3)]
result = [[0 for i in range(len(a[0]))] for j in range(len(b))]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
result[i][j] += a[i][k] * b[k][j]
for i in result:
for j in i:
print(f"{j:3}",end=" ")
print()
# Explanation:
# j goes from 0 to 2 (for each row),
# i goes from 0 to 2 (for each element in the row),
# Formula: i + j*3 + 1 generates:
# Row 0: 0+0+1, 1+0+1, 2+0+1 → [1, 2, 3]
# Row 1: 0+3+1, 1+3+1, 2+3+1 → [4, 5, 6]
# Row 2: 0+6+1, 1+6+1, 2+6+1 → [7, 8, 9]