如何使用 Numpy 建立一個單位矩陣?
在此程式中,我們將列印一個 nxn 大小的單位矩陣,其中 n 將從使用者那裡獲取。我們應當在 numpy 庫中使用 identity() 函式,該函式將維度和元素的資料型別作為引數
演算法
Step 1: Import numpy. Step 2: Take dimensions as input from the user. Step 3: Print the identity matrix using numpy.identity() function.
示例程式碼
import numpy as np
dimension = int(input("Enter the dimension of identitiy matrix: "))
identity_matrix = np.identity(dimension, dtype="int")
print(identity_matrix)輸出
Enter the dimension of identitiy matrix: 5 [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP