Can we sum a series of values,even some of them are "NaN"?If i want to do it,how can i do?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
yang-En Hsiao
am 26 Mär. 2019
Kommentiert: Walter Roberson
am 26 Mär. 2019
I get a series answer of optimal problem in several times,and i want to sum of them and average them,however, some of them are "NaN",can i or how to write a code to ignore those "NaN" and sum the others which is not "NaN" ?
The version of matlab is 2015a
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Mär. 2019
You did not indicate your MATLAB version.
For a couple of years now, you have been able to write sum(TheArray, 'omitnan') .
Before that, the easiest approach was to use the Statistics toolbox nansum()
mean() these days also has 'omitnan' flag, and in older versions the Stats tool was nanmean()
2 Kommentare
Walter Roberson
am 26 Mär. 2019
That last line returns 0 for me. A(end) is Nan, and adding nan to each A(1:9) entry gives nan for each result, so you would be applying nansum to an all-nan vector, and the result of that is going to be 0.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!