How to compute mean value, ignoring NaN values in MATLAB

Let's suppose you have data similar with the following example, and you want to compute the mean value from the data:

data = [1 2 nan 3  nan 10]

Compute the mean value

  1. If you have the Stats toolbox, use nanmean:

    nanmean(data)
  2. Otherwise, here is a line of code that you can use:

    mean(data(~isnan(data)))

Other articles:

How to save the results of profile in MATLAB

How to profile only user created functions in MATLAB