Discrepancy in sparse matrix math, when NaN's present

109 Ansichten (letzte 30 Tage)
Matt J
Matt J am 27 Okt. 2025 um 20:22
Bearbeitet: Matt J am 30 Okt. 2025 um 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 Kommentare
Matt J
Matt J am 28 Okt. 2025 um 18:38
Bearbeitet: Matt J am 28 Okt. 2025 um 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 am 28 Okt. 2025 um 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 30 Okt. 2025 um 12:06
Bearbeitet: Matt J am 30 Okt. 2025 um 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 Kommentare
Paul
Paul am 30 Okt. 2025 um 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 am 30 Okt. 2025 um 12:15
Bearbeitet: Matt J am 30 Okt. 2025 um 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."

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by