使用多維陣列的Python程式來新增兩個矩陣


矩陣是由許多按行和列排列的數字組成的二維陣列。兩個矩陣的加法是將兩個矩陣的對應元素相加並將和放在結果矩陣的對應位置的過程。只有當兩個矩陣的行數和列數相等時,這才是可能的。

在Python中,多維陣列是使用列表或NumPy陣列建立的。列表資料結構可以接受列表作為元素,因此我們可以輕鬆地建立矩陣。此外,NumPy模組提供了許多用於處理多維陣列的方法。

輸入輸出場景

兩個矩陣的加法

[a11, a12, a13]	   [b11, b12, b13]		[a11+b11, a12+b12, a13+b13]
[a21, a22, a23]  + [b21, b22, b23]	=	[a21+b21, a22+b22, a23+b23]
[a31, a32, a33]	   [b31, b32, b33]		[a31+b31, a32+b32, a33+b33]

在這篇文章中,我們將瞭解如何在python中使用多維陣列新增兩個矩陣。

使用for迴圈

在這裡,我們將使用巢狀for迴圈來遍歷給定輸入矩陣的每一行和每一列。在每次迭代中,我們將新增兩個輸入矩陣的對應元素並將它們儲存在結果矩陣中。

示例

# Defining the matrix using multidimensional arrays
matrix_a = [[1,2,3],
            [4 ,5,6],
            [7 ,8,9]]
 
matrix_b = [[1,2,3],
            [4 ,5,6],
            [7 ,8,9]]

#function for displaying matrix
def display(matrix):
   for row in matrix:
      print(row)
   print()

# Display two input matrices
print('The first matrix is defined as:') 
display(matrix_a)
print('The second matrix is defined as:')
display(matrix_b)

# Initializing Matrix with all 0s
result = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]

# Add two matrices 
for i in range(len(matrix_a)): 

   # iterate through rows 
   for j in range(len(matrix_a[0])):

      # iterate through columns
      result[i][j] = matrix_a[i][j] + matrix_b[i][j]

print('The addition of two matrices is:')
display(result)

輸出

The first matrix is defined as:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

The second matrix is defined as:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

The addition of two matrices is:
[2, 4, 6]
[8, 10, 12]
[14, 16, 18]

兩個輸入矩陣的對應元素的和儲存在我們最初用全零建立的結果矩陣中。

使用列表推導式

列表推導式提供了構建列表的最短語法,無需在for迴圈之前初始化空列表即可逐個追加值。

示例

此示例與前一個示例的工作方式類似,但區別在於這裡我們使用了列表推導式,而不是用全零建立結果矩陣。

# Defining the matrix using multidimensional arrays
matrix_a = [[1,2,5],
            [1,0,6],
            [9,8,0]]
 
matrix_b = [[0,3,5],
            [4,6,9],
            [1,8,0]]

#function for displaying matrix
def display(matrix):
   for row in matrix:
      print(row)
   print()

# Display two input matrices
print('The first matrix is defined as:') 
display(matrix_a)
print('The second matrix is defined as:')
display(matrix_b)

# Add two matrices 
result = [[matrix_a[i][j] + matrix_b[i][j]  for j in range(len(matrix_a[0]))] for i in range(len(matrix_a))]    

print('The addition of two matrices is:')
display(result)

輸出

The first matrix is defined as:
[1, 2, 5]
[1, 0, 6]
[9, 8, 0]

The second matrix is defined as:
[0, 3, 5]
[4, 6, 9]
[1, 8, 0]

The addition of two matrices is:
[1, 5, 10]
[5, 6, 15]
[10, 16, 0]

使用NumPy陣列

Python中的NumPy模組具有許多用於處理多維陣列的內建函式。透過使用這些陣列,我們可以輕鬆地新增兩個矩陣。

示例

在這個例子中,我們將使用numpy.array()方法建立兩個多維陣列。然後在兩個陣列之間應用加法運算子。

import numpy as np

# Defining the matrix using numpy array
matrix_a = np.array([[1,2,5], [1,0,6], [9,8,0]])
matrix_b = np.array([[0,3,5], [4,6,9], [1,8,0]])

# Display two input matrices
print('The first matrix is defined as:') 
print(matrix_a)

print('The second matrix is defined as:')
print(matrix_b)

# Add two matrices
result = matrix_a + matrix_b

print('The addition of two matrices is:')
print(result)

輸出

The first matrix is defined as:
[[1 2 5]
 [1 0 6]
 [9 8 0]]
The second matrix is defined as:
[[0 3 5]
 [4 6 9]
 [1 8 0]]
The addition of two matrices is:
[[ 1  5 10]
 [ 5  6 15]
 [10 16  0]]

我們簡單地在numpy陣列matrix_a、matrix_b之間應用加法運算子(+)來新增多維陣列。

更新於:2023年5月15日

891 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告