Given the matrix X in dimension of MxN. I want to create a diagonal matrix for every row of X. The result should be in MxNxN. How to do it efficiently? Thank you!
out = np.zeros((m, n, n)) out[:, np.arange(n), np.arange(n)] = X