Python Practice › NumPy Programs › Program 34
Diagonal matrix
Write a NumPy program to create a 2 × 2 zero matrix with 7 and 8 on the main diagonal.
Output
[[7 0] [0 8]]
Approach
np.diag([7, 8]).
Solution
import numpy as npprint(np.diag([7, 8]))Output
[[7 0] [0 8]]
Learn the concept
New to this topic? Read the lesson, then come back.