有没有一种快速的方法可以将列/行添加到稀疏矩阵中?

a = sparse([1,2],[1,2],[1,1])
2x2 sparse matrix with 2 Int64 entries:
[1, 1]  =  1
[2, 2]  =  1

我怎么能添加 f.x
a[3,3] = 1

无需取出每个值并重新初始化它

最佳答案

您可以使用 Julia 的常规矩阵串联功能来执行此操作,例如

julia> a = sparse([1,2],[1,2],[1,1])
2x2 sparse matrix with 2 Int64 entries:
    [1, 1]  =  1
    [2, 2]  =  1

julia> b = sparse([0 0])
1x2 sparse matrix with 0 Int64 entries:

julia> c = sparse([0 ; 0 ; 1])
3x1 sparse matrix with 1 Int64 entries:
    [3, 1]  =  1

julia> d = [[a ; b] c]
3x3 sparse matrix with 3 Int64 entries:
    [1, 1]  =  1
    [2, 2]  =  1
    [3, 3]  =  1

julia> full(d)
3x3 Array{Int64,2}:
 1  0  0
 0  1  0
 0  0  1

关于insert - Julia - 向稀疏矩阵添加行/列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38017242/

10-11 01:46