a=np.zeros((3,3,3))
b=np.arange(3)
c=np.arange(9).reshape(3,3)
I wanna put the elements of the array b
or c
along the diagonal (or above/below the diagonal) of the 3d matrix (tensor) a
with respect to a specific axis.
I tired numpy.diagflat
, but it only works for 2d matrix.
For instance, how to make the following matrix?
array([[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]]])