Given a 3D ndarray z
with shape (k,n,n)
, is it possbile without using iteration to fill the diagonals of the k nxn matrices with values given by a 2D ndarray v
with shape (k,n)
?
For example, the result of the operation should be the same as looping over k matrices:
z = np.zeros((3,10,10))
v = np.arange(30).reshape((3,10))
for i in range(len(z)):
np.fill_diagonal(z[i], v[i])
Is there an way to do this without repeatedly calling np.fill_diagonal
inside a loop? If possible, I would prefer a solution that can be applied to arrays of higher dimensions as well, where z.shape == (a,b,c,...,k,n,n)
and v.shape = (a,b,c,...,k,n)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…