1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " code" ,
5
+ "execution_count" : 13 ,
6
+ "id" : " b0452902" ,
7
+ "metadata" : {},
8
+ "outputs" : [
9
+ {
10
+ "name" : " stdout" ,
11
+ "output_type" : " stream" ,
12
+ "text" : [
13
+ " [[10, 11, 12], [13, 14, 15], [16, 17, 18]]\n " ,
14
+ " \n " ,
15
+ " Printing Matrix A\n " ,
16
+ " Matrix A:\n " ,
17
+ " \n " ,
18
+ " 10 11 12\n " ,
19
+ " 13 14 15\n " ,
20
+ " 16 17 18\n " ,
21
+ " \n " ,
22
+ " Matrix B:\n " ,
23
+ " 1 2 3\n " ,
24
+ " 4 5 6\n " ,
25
+ " 7 8 9 \n " ,
26
+ " \n " ,
27
+ " Matrix D:\n " ,
28
+ " 0 0 0 \n " ,
29
+ " 0 0 0\n " ,
30
+ " 0 0 0\n " ,
31
+ " \n " ,
32
+ " Subtraction of matrices A and B\n " ,
33
+ " [9, 9, 9]\n " ,
34
+ " [9, 9, 9]\n " ,
35
+ " [9, 9, 9]\n " ,
36
+ " \n " ,
37
+ " Sum of matrices A and B:\n " ,
38
+ " 11 13 15\n " ,
39
+ " 17 19 21\n " ,
40
+ " 23 25 27\n " ,
41
+ " \n " ,
42
+ " The sum of the diagonal elements of matrix A is: 42\n "
43
+ ]
44
+ }
45
+ ],
46
+ "source" : [
47
+ " \"\"\"\n " ,
48
+ " Matrices:\n " ,
49
+ " \n " ,
50
+ " Initializing the two matrices and the result matrix\n " ,
51
+ " Using nested for loops to subtract matching elements\n " ,
52
+ " \"\"\"\n " ,
53
+ " \n " ,
54
+ " #Practical Example: Subtraction of 3x3 Matrices in Python\n " ,
55
+ " \n " ,
56
+ " #Matrix subtraction, like addition, is an element operation\n " ,
57
+ " #a element. To subtract two matrices, they must have the same dimensions. \n " ,
58
+ " #The result is a new matrix with the same dimension, where each element is the difference\n " ,
59
+ " #of the corresponding elements of the two original matrices.\n " ,
60
+ " \n " ,
61
+ " #Initializing the two matrices and the result matrix:\n " ,
62
+ " \n " ,
63
+ " # Initialize matrix A with predefined values\n " ,
64
+ " A = [[10, 11, 12], [13, 14, 15], [16, 17, 18]]\n " ,
65
+ " print(A)\n " ,
66
+ " \n " ,
67
+ " # Print a line break followed by a title for matrix A\n " ,
68
+ " print(\"\\ nPrinting Matrix A\" )\n " ,
69
+ " \n " ,
70
+ " # Print the title for array A\n " ,
71
+ " print(\" Matrix A:\\ n\" )\n " ,
72
+ " \n " ,
73
+ " # Start a loop to traverse the rows of the matrix\n " ,
74
+ " for line in range(3): \n " ,
75
+ " \n " ,
76
+ " # Starts an internal loop to traverse the columns of the matrix\n " ,
77
+ " for column in range(3): \n " ,
78
+ " \n " ,
79
+ " # Print the value of the current element of the array followed by two spaces, without breaking the line\n " ,
80
+ " print(f\" {A[line][column]}\" , end=\" \" ) \n " ,
81
+ " \n " ,
82
+ " # Print a line break after printing all the elements of a complete line of the matrix\n " ,
83
+ " print() \n " ,
84
+ " \n " ,
85
+ " \n " ,
86
+ " # Initialize matrix B with predefined values\n " ,
87
+ " B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n " ,
88
+ " \n " ,
89
+ " # Print a line break followed by a title for matrix B\n " ,
90
+ " print(\"\\ nMatrix B:\" )\n " ,
91
+ " \n " ,
92
+ " # Start a loop to traverse the rows of the matrix\n " ,
93
+ " for line in range(3): \n " ,
94
+ " \n " ,
95
+ " # Starts an internal loop to traverse the columns of the matrix\n " ,
96
+ " for column in range(3): \n " ,
97
+ " \n " ,
98
+ " # Print the value of the current element of the array followed by two spaces, without breaking the line\n " ,
99
+ " print(f\" {B[row][column]}\" , end=\" \" ) \n " ,
100
+ " \n " ,
101
+ " # Print a line break after printing all the elements of a complete line of the matrix\n " ,
102
+ " print() \n " ,
103
+ " \n " ,
104
+ " \n " ,
105
+ " # Initialize matrix D with all elements being zero\n " ,
106
+ " D = [\n " ,
107
+ " [0, 0, 0], \n " ,
108
+ " [0, 0, 0], \n " ,
109
+ " [0, 0, 0]\n " ,
110
+ " ]\n " ,
111
+ " \n " ,
112
+ " # Print the title for array D\n " ,
113
+ " print(\"\\ nMatrix D:\" )\n " ,
114
+ " \n " ,
115
+ " # Start a loop to traverse the rows of the matrix\n " ,
116
+ " for line in range(3):\n " ,
117
+ " \n " ,
118
+ " # Starts an internal loop to traverse the columns of the matrix\n " ,
119
+ " for column in range(3):\n " ,
120
+ " \n " ,
121
+ " # Print the value of the current element of the array followed by two spaces, without breaking the line\n " ,
122
+ " print(f\" {D[row][column]}\" , end=\" \" )\n " ,
123
+ " \n " ,
124
+ " # Print a line break after printing all the elements of a complete line of the matrix\n " ,
125
+ " print()\n " ,
126
+ " \n " ,
127
+ " \n " ,
128
+ " # Subtraction of matrices A and B\n " ,
129
+ " \n " ,
130
+ " print(\"\\ nSubtraction of matrices A and B\" )\n " ,
131
+ " # Start a loop to traverse the rows of the matrix\n " ,
132
+ " for line in range(3): \n " ,
133
+ " \n " ,
134
+ " # Starts an internal loop to traverse the columns of the matrix\n " ,
135
+ " for column in range(3): \n " ,
136
+ " \n " ,
137
+ " # Subtracts the value in matrix B from the corresponding value in matrix A and stores the result in matrix D\n " ,
138
+ " D[line][column] = A[line][column] - B[line][column]\n " ,
139
+ " \n " ,
140
+ " # Start a loop to go through each row of matrix D\n " ,
141
+ " for line in D:\n " ,
142
+ " \n " ,
143
+ " # Print the current row of matrix D\n " ,
144
+ " print(line)\n " ,
145
+ " \n " ,
146
+ " \n " ,
147
+ "print()
0 commit comments