Diagonalize 2d matrix along one axis in numpy
问题 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! 回答1: out = np.zeros((m, n, n)) out[:, np.arange(n), np.arange(n)] = X 来源: https://stackoverflow.com/questions/41915720/diagonalize-2d-matrix-along-one-axis-in-numpy