# How to update individual sparse matrices mapped to sparse diagonal-matrix efficiently?

**URL:** <https://discourse.julialang.org/t/how-to-update-individual-sparse-matrices-mapped-to-sparse-diagonal-matrix-efficiently/92900>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 12, 2023, 10:25pm UTC](https://discourse.julialang.org/t/how-to-update-individual-sparse-matrices-mapped-to-sparse-diagonal-matrix-efficiently/92900 "2023-01-12T22:25:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 12, 2023, 10:25pm UTC](https://discourse.julialang.org/t/how-to-update-individual-sparse-matrices-mapped-to-sparse-diagonal-matrix-efficiently/92900/1 "2023-01-12T22:25:52Z")

</div>

I have a simple MWE below that includes a diagonal sparse matrix `A` and its sub-diagonal matrices `A_1,A_2,A_3`. Assuming that `A` is going to be updated in values only (pattern is fixed), what is the efficient way to update `A_1,A_2,A_3` correspondingly cause the below code for updating them is not efficient by using `nzval` field of the sparse matrices?

```julia
#### Define section
A = sparse([100.0 200 0 0 0 0 0; 16 17 0 0 0 0 0; 0 0 300 4 5 0 0; 0 0 6 7 8 0 0; 0 0 13 14 15 0 0; 0 0 0 0 0 9 10; 0 0 0 0 0 11 12]);
n0=1;n1=3;n2=6;n3=7;
A_1 = A[n0:n1-1,n0:n1-1];
A_2 = A[n1:n2-1,n1:n2-1];
A_3 = A[n2:n3,n2:n3];

#### Update section
# First, update A values
# Second, update the individual matrices
A_1 .= A[n0:n1-1,n0:n1-1];
A_2 .= A[n1:n2-1,n1:n2-1];
A_3 .= A[n2:n3,n2:n3];

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 12, 2023, 11:01pm UTC](https://discourse.julialang.org/t/how-to-update-individual-sparse-matrices-mapped-to-sparse-diagonal-matrix-efficiently/92900/2 "2023-01-12T23:01:35Z")

</div>

Maybe just use `@views`:

```julia
@views begin
    A_1 = A[n0:n1-1,n0:n1-1];
    A_2 = A[n1:n2-1,n1:n2-1];
    A_3 = A[n2:n3,n2:n3];
end

```

Then these will be views, not copies, and when you update `A` the changes will be immediately reflected in `A_1` etcetera.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 12, 2023, 11:11pm UTC](https://discourse.julialang.org/t/how-to-update-individual-sparse-matrices-mapped-to-sparse-diagonal-matrix-efficiently/92900/3 "2023-01-12T23:11:15Z")

</div>

Thanks for your reply.

Yes, but actually, I need to use these matrices with `KLU` which makes conflict with that as below. So, I am trying to seek another way manually.

```julia
A = sprand(10,10, 0.5)
julia> klu(@view(A[1:5,1:5]))
ERROR: MethodError: no method matching klu(::SubArray{Float64, 2, SparseMatrixCSC{Float64, Int64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false})

```
