Discrepancy in sparse matrix math, when NaN's present

77 Ansichten (letzte 30 Tage)
Matt J
Matt J am 27 Okt. 2025 um 20:22
Kommentiert: dpb am 28 Okt. 2025 um 19:54
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 vor etwa 3 Stunden
Bearbeitet: Matt J vor etwa 3 Stunden
@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 vor etwa 2 Stunden
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.

Antworten (0)

Kategorien

Mehr zu Sparse Matrices 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