Julia-向稀疏矩阵添加行/列 [英] Julia - Adding rows/columns to sparse matrices

查看:129
本文介绍了Julia-向稀疏矩阵添加行/列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以快速添加列/行以稀疏矩阵?

Is there a quick way to add columns/rows to sparse matrices?

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

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

without pulling out every value and reinitializing it

推荐答案

您可以使用Julia的矩阵常规连接功能来实现此功能,例如

You can do this with Julia's regular concatenation features for matrices, e.g.

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

这篇关于Julia-向稀疏矩阵添加行/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆