Main Content


(Not recommended) Maximum, ignoring NaN values

nanmax is not recommended. Use the MATLAB® function max instead. With the max function, you can specify whether to include or omit NaN values for the calculation. For more information, see Compatibility Considerations.


y = nanmax(X) is the maximum max of X, computed after removing NaN values.

For vectors x, nanmax(x) is the maximum of the remaining elements, once NaN values are removed. For matrices X, nanmax(X) is a row vector of column maxima, once NaN values are removed. For multidimensional arrays X, nanmax operates along the first nonsingleton dimension.

y = nanmax(X,[],dim) operates along the dimension dim of X.


[y,indices] = nanmax(___) also returns the row indices of the maximum values for each column in the vector indices.


y = nanmax(X,[],'all') returns the maximum of all elements of X, computed after removing NaN values.

y = nanmax(X,[],vecdim) returns the maximum over the dimensions specified in the vector vecdim, computed after removing NaN values. Each element of vecdim represents a dimension of the input array X. The output y has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and y. For example, if X is a 2-by-3-by-4 array, then nanmax(X,[],[1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the maximum of the elements on the corresponding page of X.

Y = nanmax(X1,X2) returns an array Y the same size as X1 and X2 with Y(i,j) = nanmax(X1(i,j),X2(i,j)). Scalar inputs are expanded to an array of the same size as the other input.


collapse all

Find the column maximum values and their indices for matrix data with missing values.

X = magic(3);
X([1 6:9]) = NaN
X = 3×3

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN

[y,indices] = nanmax(X)
y = 1×3

     4     5   NaN

indices = 1×3

     3     2     1

Find the maximum of all the values in an array, ignoring missing values.

Create a 2-by-5-by-3 array X with some missing values.

X = reshape(1:30,[2 5 3]);
X([10:12 25]) = NaN
X = 
X(:,:,1) =

     1     3     5     7     9
     2     4     6     8   NaN

X(:,:,2) =

   NaN    13    15    17    19
   NaN    14    16    18    20

X(:,:,3) =

    21    23   NaN    27    29
    22    24    26    28    30

Find the maximum of the elements of X.

y = nanmax(X,[],'all')
y = 30

Extended Capabilities

Version History

Introduced before R2006a

collapse all

R2020b: nanmax is not recommended

nanmax is not recommended. Use the MATLAB function max instead. There are no plans to remove nanmax.

To update your code, change instances of the function name nanmax to max. You do not need to change the input arguments. If you want to include NaN values, then specify the 'includenan' option for the nanflag input argument.

The max function has these advantages over the nanmax function:

  • max offers more extended capabilities for supporting tall arrays, GPU arrays, distribution arrays, C/C++ code generation, and GPU code generation.

  • When you specify the 'linear' option, max returns the linear index into the input array that corresponds to the maximum value.

See Also