溫馨提示×

Python Numpy的矩陣計算技巧

小樊
83
2024-07-15 18:39:37
欄目: 編程語言

  1. 創(chuàng)建矩陣:
import numpy as np

# 創(chuàng)建一個2x3的矩陣
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
  1. 矩陣加法和減法:
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# 矩陣加法
result_add = matrix1 + matrix2
print(result_add)

# 矩陣減法
result_sub = matrix1 - matrix2
print(result_sub)
  1. 矩陣乘法:
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# 矩陣乘法
result_mul = np.dot(matrix1, matrix2)
print(result_mul)
  1. 矩陣轉置:
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# 矩陣轉置
result_transpose = matrix.T
print(result_transpose)
  1. 矩陣求逆:
matrix = np.array([[1, 2], [3, 4]])

# 求矩陣的逆
result_inv = np.linalg.inv(matrix)
print(result_inv)

這些是一些常用的矩陣計算技巧,希望對你有幫助!

0