Skip to content

Commit 015f060

Browse files
authored
added program of matrix multiplication & transpose
1 parent 83b148a commit 015f060

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Program to transpose a matrix using a nested loop
2+
3+
X = [[12,7],
4+
[4 ,5],
5+
[3 ,8]]
6+
7+
result = [[0,0,0],
8+
[0,0,0]]
9+
10+
# iterate through rows
11+
for i in range(len(X)):
12+
# iterate through columns
13+
for j in range(len(X[0])):
14+
result[j][i] = X[i][j]
15+
16+
for r in result:
17+
print(r)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Program to multiply two matrices using nested loops
2+
3+
# 3x3 matrix
4+
X = [[12,7,3],
5+
[4 ,5,6],
6+
[7 ,8,9]]
7+
# 3x4 matrix
8+
Y = [[5,8,1,2],
9+
[6,7,3,0],
10+
[4,5,9,1]]
11+
# result is 3x4
12+
result = [[0,0,0,0],
13+
[0,0,0,0],
14+
[0,0,0,0]]
15+
16+
# iterate through rows of X
17+
for i in range(len(X)):
18+
# iterate through columns of Y
19+
for j in range(len(Y[0])):
20+
# iterate through rows of Y
21+
for k in range(len(Y)):
22+
result[i][j] += X[i][k] * Y[k][j]
23+
24+
for r in result:
25+
print(r)

0 commit comments

Comments
 (0)