Maximum, ignoring NaN values


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

Alternative Functionality

Instead of using nanmax, you can use the MATLAB® function max with the input argument nanflag specified as the value 'omitnan'.

Extended Capabilities

See Also

| |

Introduced before R2006a