# Modifying a sparse matrix non-zero indices

**URL:** <https://discourse.julialang.org/t/modifying-a-sparse-matrix-non-zero-indices/18950>\
**Category:** Performance\
**Created:** [December 23, 2018, 10:14pm UTC](https://discourse.julialang.org/t/modifying-a-sparse-matrix-non-zero-indices/18950 "2018-12-23T22:14:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [December 23, 2018, 10:14pm UTC](https://discourse.julialang.org/t/modifying-a-sparse-matrix-non-zero-indices/18950/1 "2018-12-23T22:14:18Z")

</div>

I’m working in a problem where I have a sparse Jacobian matrix with 16 values in each row. I would like to update the matrix by just modifying its non-zero values. The problem is I would like to update both the non-zero values and indices, but the indices are immutable. How should I approach this?

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 25, 2018, 1:30am UTC](https://discourse.julialang.org/t/modifying-a-sparse-matrix-non-zero-indices/18950/2 "2018-12-25T01:30:07Z")

</div>

You can update the `colptr` and `rowval` vectors directly, as in the example below, but it’s not very user friendly.

```julia
julia> S = sprand(3, 3, 0.25)
3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [2, 1] = 0.00723938
  [3, 3] = 0.9488

julia> Matrix(S)
3×3 Array{Float64,2}:
 0.0 0.0 0.0
 0.00723938 0.0 0.0
 0.0 0.0 0.9488

julia> S.colptr
4-element Array{Int64,1}:
 1
 2
 2
 3

julia> S.rowval
2-element Array{Int64,1}:
 2
 3

julia> S.colptr[3] = 3; S.rowval[2] = 1;

julia> S
3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [2, 1] = 0.00723938
  [1, 2] = 0.9488

julia> Matrix(S)
3×3 Array{Float64,2}:
 0.0 0.9488 0.0
 0.00723938 0.0 0.0
 0.0 0.0 0.0

```
