6

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!

Chris Zhang
  • 113
  • 1
  • 6

1 Answers1

6
out = np.zeros((m, n, n))
out[:, np.arange(n), np.arange(n)] = X
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99