MATLAB: Can we sum a series of values,even some of them are “NaN”?If i want to do it,how can i do

MATLABnansum

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

Best Answer

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()