ในโปรแกรมนี้ เราจะพิมพ์ Identity Matrix ขนาด n x n โดย n จะรับจากผู้ใช้ เราจะใช้ฟังก์ชัน identity() ในไลบรารี NumPy ซึ่งรับพารามิเตอร์เป็นมิติและชนิดข้อมูลขององค์ประกอบ
อัลกอริทึม
ขั้นตอนที่ 1: นำเข้า numpy ขั้นตอนที่ 2: รับมิติจากผู้ใช้ ขั้นตอนที่ 3: พิมพ์ Identity Matrix ด้วยฟังก์ชัน numpy.identity()
ตัวอย่างโค้ด
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]]