Python Practice › NumPy Programs › Program 34

Diagonal matrix

EasyNumPy Programs · 34 of 100

Write a NumPy program to create a 2 × 2 zero matrix with 7 and 8 on the main diagonal.

Advertisement
Output
[[7 0]
 [0 8]]

Approach

  1. np.diag([7, 8]).

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment