I have matlab 2013b and all of a sudden the following code for median with omitnan does not work. Can anyone help troubleshoot?

2 views (last 30 days)
A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; M = median(A,'omitnan')
M =
1.7700 -0.0050 3.9800 -2.9500 NaN 0.3400 NaN 0.1900

Answers (2)

Star Strider
Star Strider on 25 May 2017
Use ‘logical indexing’ and the isnan function (as ~isnan):
A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
M_omitnan = median(A, 'omitnan')
M_notisnan = median(A(~isnan(A)))
M_omitnan =
0.265
M_notisnan =
0.265
That it is ‘suddenly not working’ means that you could be ‘overshadowing’ it with a variable or usdr function named ‘median’. To determine that, run this line from your Command Window (or a script):
which median -all
You should only get returns on the ‘C:\Program Files\MATLAB\R2013b\toolbox\’ path. Anything else will reveal where the problem is.

Fangjun Jiang
Fangjun Jiang on 26 May 2017
You must be mistaken about the MATLAB version. The "nanflag" option for the median() function is only available for R2015a and later. See the document link below. Change the hyper link to R2015a to see the difference.
https://www.mathworks.com/help/releases/R2014b/matlab/ref/median.html?searchHighlight=median

Community Treasure Hunt

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

Start Hunting!