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.
What is the output of the following code?arr = numpy.array([numpy.arange(5), numpy.arange(1, 6)])arr A 2D array of numbers from 0 to 5 in the first row and 1 to 6 in the second row A 2D array of numbers from 0 to 4 in the first row and 1 to 6 in the second row A 2D array of numbers from 0 to 4 in the first row and 1 to 5 in the second row None of the above
Create an integer array of size 10, where all the values should be 0 but the fifth value should be 1.Print the elements of array.
Spiral MatrixWrite a Python program to print spiral matrix for a given matrix.Constraints:1<R<101<C<10Example:23number of elements in the array:6elements of array:-1 2 3 4 5 61 2 3 6 5 4
Given a 2D integer array size (4, 5) with name input_?Print elements of highlighted matrix Output Format :
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.