Create a matrix having diagonal elements as 1 and all other elements as 0 of size (5, 6).Print the Numpy array.
Question
Create a matrix having diagonal elements as 1 and all other elements as 0 of size (5, 6). Print the Numpy array.
Solution
Sure, here is how you can create a matrix with diagonal elements as 1 and all other elements as 0 of size (5, 6) using Numpy in Python:
import numpy as np
# Create a matrix of zeros with size (5,6)
matrix = np.zeros((5, 6))
# Change the diagonal elements to 1
for i in range(min(matrix.shape)):
matrix[i][i] = 1
print(matrix)
When you run this code, it will print a 5x6 matrix where the diagonal elements are 1 and all other elements are 0.
Similar Questions
Given a 2D list, create a numpy 2D array using it.Note: Given 2D list is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Print the Numpy array.
Given a 2D integer array size (4, 5) with name input_?Print elements of highlighted matrix Output Format :
What is the output of the following Python code?for i in range(5,0,-1): print(i)5 4 3 2 11 2 3 4 55 5 5 5 51 1 1 1 1
What is a correct syntax to create a NumPy array?np.createArray([4,5,6])np.numpyArray([4,5,6])np.array([4,5,6])np.create_array([4,5,6])
Create an array with values ranging consecutively from 9 to 49 (both inclusive).Print the Numpy array.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.