| Statistics Toolbox | ![]() |
Functions for Data with Missing Values (NaNs)
Most real-world data sets have one or more missing elements. It is convenient to code missing entries in a matrix as NaN (Not a Number).
m = magic(3); m([1 5]) = [NaN NaN] m = NaN 1 6 3 NaN 7 4 9 2
Any arithmetic operation that involves the missing values in this matrix yields NaN, as below.
sum(m) ans = NaN NaN 15
Removing cells with NaN would destroy the matrix structure. Removing whole rows that contain NaN would discard real data. Instead, the Statistics Toolbox has a variety of functions that are similar to other MATLAB functions, but that treat NaN values as missing and therefore ignore them in the calculations.
nansum(m)
ans =
7 10 13
| NaN Functions | |
nanmax |
Maximum ignoring NaNs |
nanmean |
Mean ignoring NaNs |
nanmedian |
Median ignoring NaNs |
nanmin |
Minimum ignoring NaNs |
nanstd |
Standard deviation ignoring NaNs |
nansum |
Sum ignoring NaNs |
In addition, other Statistics Toolbox functions operate only on the numeric values, ignoring NaNs. These include iqr, kurtosis, mad, prctile, range, skewness, and trimmean.
| Measures of Dispersion | Function for Grouped Data | ![]() |