Inserting 0 in sparse matrices without changing sparsity pattern?

23 vues (au cours des 30 derniers jours)
Anders Melander
Anders Melander le 5 Avr 2024 à 11:09
Modifié(e) : Bruno Luong le 5 Avr 2024 à 13:58
Hello,
In my code I have very large sparse diagonal matrices, where the values of the diagonal change over time.
The code is very long, so I instead have an example of the issue profiled as seen below. If the new diagonal b contains zeros (line 17, 30% zeros), it is much slower compared to if b contains no zeros. In my own code b usually has between 5-30% zeros. My guess is this is because MATLAB reformats the sparse matrix when a zero is inserted(i.e. the element is deleted, indices are changed). Is it possible to tell MATLAB to not change the sparse matrix, but instead treat inserted zeros as any other value? By this I mean that the value 0 is actually saved in the diagonal, despite it being a sparse matrix.

Réponse acceptée

Bruno Luong
Bruno Luong le 5 Avr 2024 à 11:21
Modifié(e) : Bruno Luong le 5 Avr 2024 à 13:58
Is it possible to tell MATLAB to not change the sparse matrix, but instead treat inserted zeros as any other value?
Short answer: No.
You can insert something that replaces 0, for example NaN or realmin and do specific treament with this value.
  1 commentaire
Anders Melander
Anders Melander le 5 Avr 2024 à 13:02
Yeah, I had already used realmin as a "hack", just wondered if I could have avoided it, but seems not.
Thanks for the answer!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by