How to change values of elements in a sparse matrix

I have the following sparse matrix:
(1,1) 1.1000
(2,2) 2.1000
(3,3) 0.1000
(4,4) 5.1000
(5,5) 0.6000
Created from a full matrix. I want to modify the sparse matrix by changing some values in the second column above. I do not want to change them in the original matrix. The reason is the full matrix is very large and the majority of elements are zeros. It takes a big portion of my computer memory (greater than 8 GB of my RAM capacity). The above matrix is only a small sample of my problem. Note that for this problem, the nonzero elements are located in the diagonal of the matrix.
Any idea?
Thanks

1 comentario

Rik
Rik el 9 de Ag. de 2018
Variable in Matlab in general don't affect each other, so you can just change the elements. What errors are you encountering?

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 9 de Ag. de 2018
Editada: James Tursa el 9 de Ag. de 2018
Not sure what your problem is. Sparse matrix elements can be changed directly just like full matrices. E.g.,
F = your full matrix
S = sparse(F); % your sparse matrix
S(3,2) = something; % change a value in S, does not affect F
F(5,2) = something else; % change a value in F, does not affect S
How many elements will you be changing?

4 comentarios

Ismaeel
Ismaeel el 9 de Ag. de 2018
Thanks, James for your reply, it works. My original matrix is an 82,000 X 82,000 diagonal matrix stored in an excel sheet. So, I need a for-loop to change these values. Thanks once again
Depending on how many values you are changing, a for-loop can take a tremendous amount of time because the entire data might be copied to a new memory block at each iteration. There might be better ways to change the elements all at once to avoid this penalty.
Ismaeel
Ismaeel el 9 de Ag. de 2018
I need to change all diagonal elements (82,000 elements).
James Tursa
James Tursa el 9 de Ag. de 2018
Editada: James Tursa el 9 de Ag. de 2018
If there are already non-zero elements in the diagonal and you are changing them to other non-zero values, then a loop probably won't hurt you since in theory it would not require large amounts of memory to be copied. But if that is not the case, then doing it all at once will probably be a better method. E.g.,
v = a vector of 82000 values for the diagonal
S = your sparse matrix
S(1:82001:end) = v; % change all the diagonals at once using linear indexing

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Ag. de 2018

Editada:

el 9 de Ag. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by