Hướng dẫn how do you add two matrices in python? - làm thế nào để bạn thêm hai ma trận trong python?

Trong chương trình này, bạn sẽ học cách thêm hai ma trận bằng cách sử dụng vòng lặp lồng nhau và khả năng hiểu danh sách tiếp theo và hiển thị nó.

Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Python sau:

  • Python cho vòng lặp
  • Danh sách Python

Trong Python, chúng ta có thể triển khai ma trận dưới dạng danh sách lồng nhau (danh sách bên trong danh sách). Chúng ta có thể coi từng phần tử như một hàng của ma trận.

Ví dụ X = [[1, 2], [4, 5], [3, 6]] sẽ đại diện cho ma trận 3x2. Hàng đầu tiên có thể được chọn là X[0] và phần tử trong hàng đầu tiên, cột đầu tiên có thể được chọn là ____10.

Chúng ta có thể thực hiện bổ sung ma trận theo nhiều cách khác nhau trong Python. Dưới đây là một vài trong số họ.

Mã nguồn: bổ sung ma trận bằng cách sử dụng vòng lặp lồng nhau

# Program to add two matrices using nested loop

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[0,0,0],
         [0,0,0],
         [0,0,0]]

# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[i][j] = X[i][j] + Y[i][j]

for r in result:
   print(r)

Đầu ra

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]

Trong chương trình này, chúng tôi đã sử dụng các vòng lặp

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 lồng nhau để lặp qua mỗi hàng và mỗi cột. Tại mỗi điểm, chúng tôi thêm các phần tử tương ứng trong hai ma trận và lưu trữ nó trong kết quả.

Mã nguồn: bổ sung ma trận bằng cách sử dụng danh sách lồng nhau

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)

Đầu ra của chương trình này giống như trên. Chúng tôi đã sử dụng sự hiểu biết danh sách lồng nhau để lặp qua từng phần tử trong ma trận.

Danh sách hiểu cho phép chúng tôi viết các mã ngắn gọn và chúng tôi phải cố gắng sử dụng chúng thường xuyên trong Python. Họ rất hữu ích.

Độ phức tạp về thời gian: O (len (x) * len (x [0])), vì chúng ta đang sử dụng vòng lặp lồng nhau để đi qua không gian ma trận. Chúng tôi đang sử dụng một ma trận kết quả là không gian thêm.

Examples: 

Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]

Phương pháp 3:  

Giải thích:-& nbsp; đầu ra của chương trình này giống như trên. Chúng tôi đã sử dụng sự hiểu biết danh sách lồng nhau để lặp qua từng phần tử trong ma trận. Danh sách hiểu cho phép chúng tôi viết các mã ngắn gọn và chúng tôi phải cố gắng sử dụng chúng thường xuyên trong Python. Họ rất hữu ích.for loop: 

Dưới đây là việc thực hiện:

Python

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

Các

Các

Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
3
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
6

[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

[[10 10 10]
[10 10 10]
[10 10 10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

[[10 10 10]
[10 10 10]
[10 10 10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
6

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 X = [[1, 2], [4, 5], [3, 6]]8X = [[1, 2], [4, 5], [3, 6]]9 X[0]0X[0]1X[0]2

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 X[0]6X = [[1, 2], [4, 5], [3, 6]]9 X[0]0X[0]1X[0]2____101____
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
03

[[10 10 10]
[10 10 10]
[10 10 10]]
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
05
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
07
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
08
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
09

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
11X = [[1, 2], [4, 5], [3, 6]]9
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
13

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
15
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
16

Đầu ra

[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Độ phức tạp về thời gian: O (len (x) * len (x [0])), vì chúng ta đang sử dụng vòng lặp lồng nhau để đi qua không gian ma trận. Chúng tôi đang sử dụng một ma trận kết quả là không gian thêm., as we are using nested loop for traversing the matrix.
Auxiliary Space: O(len(X) * len(X[0])), as we are using a result matrix which is extra space.

Phương pháp 2:

  1. Giải thích:-& nbsp; Trong chương trình này, chúng tôi đã sử dụng lồng nhau cho các vòng lặp để lặp qua mỗi hàng và mỗi cột. Tại mỗi điểm, chúng tôi thêm các phần tử tương ứng trong hai ma trận và lưu trữ nó trong kết quả.In this program we have used nested for loops to iterate through each row and each column. At each point we add the corresponding elements in the two matrices and store it in the result.
  2. Sử dụng danh sách lồng nhau hiểu được: Trong Python, chúng ta có thể triển khai một ma trận làm danh sách lồng nhau (danh sách bên trong danh sách). Chúng ta có thể coi từng phần tử như một hàng của ma trận. & Nbsp;list comprehension : In Python, we can implement a matrix as nested list (list inside a list). We can treat each element as a row of the matrix. 

Dưới đây là việc thực hiện:

Python

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

Các

Các

Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
3
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
6

[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
69
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
08
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
71
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 X[0]6X = [[1, 2], [4, 5], [3, 6]]9 X[0]0

X[0]1X[0]2

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
01
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
80____11 X = [[1, 2], [4, 5], [3, 6]]8X = [[1, 2], [4, 5], [3, 6]]9 X[0]0X[0]1X[0]2

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
11X = [[1, 2], [4, 5], [3, 6]]9
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
13

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
15
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
16

Đầu ra

[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Độ phức tạp về thời gian: O (len (x) * len (x [0])), vì chúng ta đang sử dụng vòng lặp lồng nhau để đi qua không gian ma trận. Chúng tôi đang sử dụng một ma trận kết quả là không gian thêm. O(len(X) * len(X[0])), as we are using nested loop for traversing the matrix.
Auxiliary Space: O(len(X) * len(X[0])), as we are using a result matrix which is extra space.

Phương pháp 2:

  1. Giải thích:-& nbsp; Trong chương trình này, chúng tôi đã sử dụng lồng nhau cho các vòng lặp để lặp qua mỗi hàng và mỗi cột. Tại mỗi điểm, chúng tôi thêm các phần tử tương ứng trong hai ma trận và lưu trữ nó trong kết quả.
    The output of this program is the same as above. We have used nested list comprehension to iterate through each element in the matrix. List comprehension allows us to write concise codes and we must try to use them frequently in Python. They are very helpful.
  2. Sử dụng danh sách lồng nhau hiểu được: Trong Python, chúng ta có thể triển khai một ma trận làm danh sách lồng nhau (danh sách bên trong danh sách). Chúng ta có thể coi từng phần tử như một hàng của ma trận. & Nbsp;zip() and sum 

Dưới đây là việc thực hiện:

Python

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

Các

Các

Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
3
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
6

[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
69
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
08
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
71
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 X[0]6X = [[1, 2], [4, 5], [3, 6]]9 X[0]0

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
15
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
62

Đầu ra

[[10, 10, 10], [10, 10, 10], [10, 10, 10]]

Độ phức tạp về thời gian: O (len (x) * len (x [0])), vì chúng ta đang sử dụng vòng lặp lồng nhau để đi qua không gian ma trận. Chúng tôi đang sử dụng một ma trận kết quả là không gian thêm., as we are using zip function.
Auxiliary Space: O(len(X) * len(X[0])), as we are using extra space.

Phương pháp 2:The zip function accepts iterator i of each element(list) of matrix, mapping them, adding them using sum(), and storing them in the map form.

Giải thích:-& nbsp; Trong chương trình này, chúng tôi đã sử dụng lồng nhau cho các vòng lặp để lặp qua mỗi hàng và mỗi cột. Tại mỗi điểm, chúng tôi thêm các phần tử tương ứng trong hai ma trận và lưu trữ nó trong kết quả.

Sử dụng danh sách lồng nhau hiểu được: Trong Python, chúng ta có thể triển khai một ma trận làm danh sách lồng nhau (danh sách bên trong danh sách). Chúng ta có thể coi từng phần tử như một hàng của ma trận. & Nbsp; numpy libraryhas a built-in overload of the operator +, that allows one to perform the addition of matrices.

Dưới đây là việc thực hiện:

Python3

Python

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
9
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

Các

Các

Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
4
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
Input :
 X= [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
 
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
 
Output :
 result= [[10,10,10],
         [10,10,10],
         [10,10,10]]
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
1
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
2
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
7
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
6
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
3
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
0

[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
2
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
3
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
69
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
08
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
71
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
1 X[0]6X = [[1, 2], [4, 5], [3, 6]]9 X[0]0

X[0]1X[0]2

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
01
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
5
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
80____11 X = [[1, 2], [4, 5], [3, 6]]8X = [[1, 2], [4, 5], [3, 6]]9 X[0]0X[0]1X[0]2

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
15
# Program to add two matrices using list comprehension

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]

for r in result:
   print(r)
62

Output:

[[10 10 10]
[10 10 10]
[10 10 10]]

Độ phức tạp về thời gian: O (len (x) * len (x [0])), vì chúng ta đang sử dụng vòng lặp lồng nhau để đi qua không gian ma trận. Chúng tôi đang sử dụng một ma trận kết quả là không gian thêm.O(len(X) * len(X[0]))
Auxiliary Space: O(len(X) * len(X[0]))

Phương pháp 3:ajay0007. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.