File tree 2 files changed +42
-0
lines changed
Beginner_Level_Python_programs
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments