-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixFunc.py
53 lines (44 loc) · 1.08 KB
/
matrixFunc.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
44
45
46
47
48
49
50
51
52
53
def matrixAdd(A, B, SUM):
# Get the number of rows and columns
rows = len(A)
cols = len(A[0])
# Add the corresponding elements of matrices A and B
for i in range(rows):
for j in range(cols):
SUM[i][j] = A[i][j] + B[i][j]
def matrixAddAsString(A, B, SUM):
# Get the number of rows and columns
rows = len(A)
cols = len(A[0])
# Add the corresponding elements of matrices A and B as strings
for i in range(rows):
for j in range(cols):
SUM[i][j] = str(A[i][j]) + str(B[i][j])
def printMatrix(matrix):
for row in matrix:
print(row)
# Example matrices A and B
A = [
[1, 2, -3],
[-4, 5, 6],
[7, 8, -9]
]
B = [
[0, 1, 2],
[3, 0, -4],
[-1, 5, 6]
]
# Initialize SUM matrix to store the result
SUM = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
# Perform matrix addition
matrixAdd(A, B, SUM)
print("Matrix Addition (numeric):")
printMatrix(SUM)
# Perform matrix addition as string concatenation
matrixAddAsString(A, B, SUM)
print("\nMatrix Addition (as strings):")
printMatrix(SUM)