解决方法
使用
scipy.sparse.spdiags
(这很多,因此最初可能会令人困惑),scipy.sparse.dia_matrix
和/或
scipy.sparse.lil_diags
.(取决于
format你想要稀疏矩阵……)
例如.使用spdiags:
import numpy as np import scipy as sp import scipy.sparse x = np.arange(10) # "0" here indicates the main diagonal... # "y" will be a dia_matrix type of sparse array,by default y = sp.sparse.spdiags(x,x.size,x.size)