Create a 2×2 zero matrix with elements on the main diagonal equal to numbers

In this python numpy program, we will create a 2×2 zero matrix with elements on the main diagonal equal to numbers.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a 2×2 zero matrix with elements on the main diagonal using np.diag().
  3. Print the output.
				
					import numpy as np
x = np.diag([7,8])
print(x)
				
			

Output :

				
					[[7 0]
 [0 8]]
				
			

reshape the 2×3 matrix into a 2×2 matrix.

calculate the sum of all elements in an array.

Leave a Comment