具有numpy和scipy的矩阵的对角矩阵

时间:2013-10-04 09:40:40

标签: python numpy scipy

我有一个矩阵(n * 1),我想用它做一个对角矩阵。 但我不能用numpy构建它。 我尝试了numpy中的每个方法,例如this

中的方法

1 个答案:

答案 0 :(得分:7)

import numpy
arr = numpy.array([1,2,3])
mat = numpy.diag(arr)
print(mat)
>>> 
[[1 0 0]
 [0 2 0]
 [0 0 3]]
相关问题