'Numpy change diagonal values in a 3D array

I have a vector with the following values: dv = array([0., 0., 1.]).

How do I diagonalize this vector into a 3D array with each element in the vector have its own diagonal:

array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]]])

So far i have tried:

import numpy as np

a = np.zeros((3,3,3))
di = np.diag_indices(3,3)
a[di] = dv

This is almost correct, but it does not change all of the elements on the diagonal.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source