Discrepancy in sparse matrix math, when NaN's present

118 vues (au cours des 30 derniers jours)
Matt J
Matt J le 27 Oct 2025 à 20:22
Modifié(e) : Matt J le 30 Oct 2025 à 12:51
I expect result1 and result2 below to be identical, but they aren't. The discrepancy must be a bug, right? I'm working in R2024b, but the Run output below shows the issue exists as well in whatever the Matlab online engine is now running.
n = 5; m = 3;
S = sparse(n, m);
v=nan(n, 1);
D=sparse(diag(v));
result1=full(v.*S) %correct
result1 = 5×3
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result2=full(D*S) %incorrect
result2 = 5×3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  8 commentaires
Matt J
Matt J le 28 Oct 2025 à 18:38
Modifié(e) : Matt J le 28 Oct 2025 à 18:39
@dpb Yes, I think there's little doubt that it has always been there. It's clear, from a performance optimization point of view, why you mgiht want sparse mtimes() to ignore NaNs. The whole idea of sparse type is to exploit the fact that 0*somethingFinite=0.If you have to worry about "somethingFinite" being non-finite, I wonder how much of the intended performance can be salvaged.
dpb
dpb le 28 Oct 2025 à 19:54
There was a similar thread on sparse performance here just a week or so ago where it ended up being full for similar reasons. I looked but didn't find it in a cursory search, iirc, it was @Steven Lord who pointed out the issue about timing.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 30 Oct 2025 à 12:06
Modifié(e) : Matt J le 30 Oct 2025 à 12:51
The response I seem to be getting from Tech Support is that this is expected behavior. NaNs are not guaranteed to propagate the same in sparse matrix multiplication as in full. In particular, sparse mtimes does not try to detect 0*NaN scenarios when the zero is sparse/implicit.
  2 commentaires
Paul
Paul le 30 Oct 2025 à 12:09
Is this expected behavior documented anywhere? I poked around a bit but didn't see anything; admittedly didn't try very hard (shouldn't have to).
Matt J
Matt J le 30 Oct 2025 à 12:15
Modifié(e) : Matt J le 30 Oct 2025 à 12:33
No, Tech Support seems to acknowledge that it isn't documented:
"I apologize for any confusion caused by the lack of documentation regarding NaN propagation in these operations.
To clarify, the time differences you are seeing between “.*” and “*” are due to underlying algorithms which handle NaN propagation differently. The same concept applies to the result of the second example that you provided. Specifically, the propagation rules between a sparse-sparse matrix multiply are different from a sparse-full matrix multiply.
So, although theoretically the results should be the same for both cases, the underlying code goes through a different algorithm and propagates NaN differently."

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by