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

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?

``````#### 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];
``````

Maybe just use `@views`:

``````@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.

1 Like

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.
``````A = sprand(10,10, 0.5)