Suppose I have a list of diagonal entries, d
, is there a simple way to create a diagonal matrix like A
? Can I specify the dimension of the result using np.diag(d)
?
import numpy as np
d = np.array([1,2])
A = np.array([[1,0,0,0], [0,2,0,0], [0,0,0,0], [0,0,0,0]])
It's simple:
A = np.zeros((4,4))
A[:2, :2] = np.diag(d)