+3 votes
in Class 12 by kratos

Write a Numpy program to create a 3x3 identity matrix, i.e. non diagonal elements are 1, the rest are 0. Replace all 0 to random number from 1 to 10

1 Answer

+2 votes
by kratos
 
Best answer

import numpy as np

Z = np.arange(9).reshape(3,3)

print (Z)

x=np.where((Z%2)==0)

for i in x:

Z[x]=np.random.randint(low=10,high=20)

print(Z)

...