# max

Maximum elements of array

## Syntax

``M = max(A)``
``M = max(A,[],"all")``
``M = max(A,[],dim)``
``M = max(A,[],vecdim)``
``M = max(A,[],___,missingflag)``
``````[M,I] = max(___)``````
``````[M,I] = max(A,[],___,"linear")``````
``C = max(A,B)``
``C = max(A,B,missingflag)``
``___ = max(___,"ComparisonMethod",method)``

## Description

example

````M = max(A)` returns the maximum elements of an array. If `A` is a vector, then `max(A)` returns the maximum of `A`.If `A` is a matrix, then `max(A)` is a row vector containing the maximum value of each column of `A`.If `A` is a multidimensional array, then `max(A)` operates along the first dimension of `A` whose size is greater than `1`, treating the elements as vectors. The size of `M` in this dimension becomes `1`, while the sizes of all other dimensions remain the same as in `A`. If `A` is an empty array whose first dimension has zero length, then `M` is an empty array with the same size as `A`.If `A` is a table or timetable, then `max(A)` returns a one-row table containing the maximum of each variable. (since R2023a) ```

example

````M = max(A,[],"all")` finds the maximum over all elements of `A`.```

example

````M = max(A,[],dim)` returns the maximum element along dimension `dim`. For example, if `A` is a matrix, then `max(A,[],2)` returns a column vector containing the maximum value of each row.```

example

````M = max(A,[],vecdim)` returns the maximum over the dimensions specified in the vector `vecdim`. For example, if `A` is a matrix, then `max(A,[],[1 2])` returns the maximum over all elements in `A` because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.```

example

````M = max(A,[],___,missingflag)` specifies whether to omit or include missing values in `A` for any of the previous syntaxes. For example, `max(A,[],"includemissing")` includes all missing values when computing the maximum. By default, `max` omits missing values.```

example

``````[M,I] = max(___)``` also returns the index into the operating dimension that corresponds to the first occurrence of the maximum value of `A`.```

example

``````[M,I] = max(A,[],___,"linear")``` also returns the linear index into `A` that corresponds to the maximum value in `A`.```

example

````C = max(A,B)` returns an array with the largest elements taken from `A` or `B`.```
````C = max(A,B,missingflag)` also specifies how to treat missing values.```
````___ = max(___,"ComparisonMethod",method)` optionally specifies how to compare elements for any of the previous syntaxes. For example, for a vector `A = [-1 2 -9]`, the syntax `max(A,[],"ComparisonMethod","abs")` compares the elements of `A` according to their absolute values and returns a maximum value of `-9`.```

## Examples

collapse all

Create a vector and compute its largest element.

```A = [23 42 37 18 52]; M = max(A)```
```M = 52 ```

Create a complex vector and compute its largest element, that is, the element with the largest magnitude.

```A = [-2+2i 4+i -1-3i]; max(A)```
```ans = 4.0000 + 1.0000i ```

Create a matrix and compute the largest element in each column.

`A = [2 8 4; 7 3 9]`
```A = 2×3 2 8 4 7 3 9 ```
`M = max(A)`
```M = 1×3 7 8 9 ```

Create a matrix and compute the largest element in each row.

`A = [1.7 1.2 1.5; 1.3 1.6 1.99]`
```A = 2×3 1.7000 1.2000 1.5000 1.3000 1.6000 1.9900 ```
`M = max(A,[],2)`
```M = 2×1 1.7000 1.9900 ```

Create a 3-D array and compute the maximum over each page of data (rows and columns).

```A(:,:,1) = [2 4; -2 1]; A(:,:,2) = [9 13; -5 7]; A(:,:,3) = [4 4; 8 -3]; M1 = max(A,[],[1 2])```
```M1 = M1(:,:,1) = 4 M1(:,:,2) = 13 M1(:,:,3) = 8 ```

To compute the maximum over all dimensions of an array, you can either specify each dimension in the vector dimension argument or use the `"all"` option.

`M2 = max(A,[],[1 2 3])`
```M2 = 13 ```
`Mall = max(A,[],"all")`
```Mall = 13 ```

Create a matrix containing `NaN` values.

`A = [1.77 -0.005 NaN -2.95; NaN 0.34 NaN 0.19]`
```A = 2×4 1.7700 -0.0050 NaN -2.9500 NaN 0.3400 NaN 0.1900 ```

Compute the maximum value of the matrix, including missing values. For matrix columns that contain any `NaN` value, the maximum is `NaN`.

`M = max(A,[],"includemissing")`
```M = 1×4 NaN 0.3400 NaN 0.1900 ```

Create a matrix `A` and compute the largest elements in each column, as well as the row indices of `A` in which they appear.

`A = [1 9 -2; 8 4 -5]`
```A = 2×3 1 9 -2 8 4 -5 ```
`[M,I] = max(A)`
```M = 1×3 8 9 -2 ```
```I = 1×3 2 1 1 ```

Create a matrix `A` and return the maximum value of each row in the matrix `M`. Use the `"linear"` option to also return the linear indices `I` such that `M = A(I)`.

`A = [1 2 3; 4 5 6]`
```A = 2×3 1 2 3 4 5 6 ```
`[M,I] = max(A,[],2,"linear")`
```M = 2×1 3 6 ```
```I = 2×1 5 6 ```
`maxvals = A(I)`
```maxvals = 2×1 3 6 ```

Create a matrix and return the largest value between each of its elements compared to a scalar.

`A = [1 7 3; 6 2 9]`
```A = 2×3 1 7 3 6 2 9 ```
```B = 5; C = max(A,B)```
```C = 2×3 5 7 5 6 5 9 ```

## Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

• If `A` is complex, then `max(A)` returns the complex number with the largest magnitude. If magnitudes are equal, then `max(A)` returns the value with the largest magnitude and the largest phase angle.

• If `A` is a scalar, then `max(A)` returns `A`.

• If `A` is a 0-by-0 empty array, then `max(A)` is as well.

If `A` has type `categorical`, then it must be ordinal.

Complex Number Support: Yes

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimension `dim` indicates the dimension whose length reduces to `1`. The `size(M,dim)` is `1`, while the sizes of all other dimensions remain the same, unless `size(A,dim)` is `0`. If `size(A,dim)` is `0`, then `max(A,dim)` returns an empty array with the same size as `A`.

Consider an `m`-by-`n` input matrix, `A`:

• `max(A,[],1)` computes the maximum of the elements in each column of `A` and returns a `1`-by-`n` row vector. • `max(A,[],2)` computes the maximum of the elements in each row of `A` and returns an `m`-by-`1` column vector. Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

Consider a 2-by-3-by-3 input array, `A`. Then `max(A,[],[1 2])` returns a 1-by-1-by-3 array whose elements are the maximums computed over each page of `A`. Additional input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. Inputs `A` and `B` must either be the same size or have sizes that are compatible (for example, `A` is an `M`-by-`N` matrix and `B` is a scalar or `1`-by-`N` row vector). For more information, see Compatible Array Sizes for Basic Operations.

• If `A` and `B` are both arrays, then they must be the same data type unless one is a `double`. In that case, the data type of the other array can be `single`, `duration`, or any integer type.

• If `A` and `B` are ordinal `categorical` arrays, they must have the same sets of categories with the same order.

• If either `A` or `B` is a table or timetable, then the other input can be an array, table, or timetable.

Complex Number Support: Yes

Missing value condition, specified as one of the values in this table.

ValueInput Data TypeDescription
`"omitmissing"`All supported data typesIgnore missing values in the input arrays, and compute the maximum over fewer points. If all elements in the operating dimension are missing, then the corresponding element in `M` is missing.
`"omitnan"``double`, `single`, `duration`
`"omitnat"``datetime`
`"omitundefined"``categorical`
`"includemissing"`All supported data types

Include missing values in the input arrays when computing the maximum. If any element in the operating dimension is missing, then the corresponding element in `M` is missing.

`"includenan"``double`, `single`, `duration`
`"includenat"``datetime`
`"includeundefined"``categorical`

Comparison method for numeric input, specified as one of these values:

• `"auto"` — For a numeric input array `A`, compare elements by `real(A)` when `A` is real, and by `abs(A)` when `A` is complex.

• `"real"` — For a numeric input array `A`, compare elements by `real(A)` when `A` is real or complex. If `A` has elements with equal real parts, then use `imag(A)` to break ties.

• `"abs"` — For a numeric input array `A`, compare elements by `abs(A)` when `A` is real or complex. If `A` has elements with equal magnitude, then use `angle(A)` in the interval (-π,π] to break ties.

## Output Arguments

collapse all

Maximum values, returned as a scalar, vector, matrix, multidimensional array, or table. `size(M,dim)` is `1`, while the sizes of all other dimensions match the size of the corresponding dimension in `A`, unless `size(A,dim)` is `0`. If `size(A,dim)` is `0`, then `M` is an empty array with the same size as `A`.

Index, returned as a scalar, vector, matrix, multidimensional array, or table. `I` is the same size as the first output.

When `"linear"` is not specified, `I` is the index into the operating dimension. When `"linear"` is specified, `I` contains the linear indices of `A` corresponding to the maximum values.

If the largest element occurs more than once, then `I` contains the index to the first occurrence of the value.

Maximum elements from `A` or `B`, returned as a scalar, vector, matrix, multidimensional array, table, or timetable. The size of `C` is determined by implicit expansion of the dimensions of `A` and `B`. For more information, see Compatible Array Sizes for Basic Operations.

The data type of `C` depends on the data types of `A` and `B`:

• If `A` and `B` are the same data type, then `C` matches the data type of `A` and `B`.

• If either `A` or `B` is `single`, then `C` is `single`.

• If either `A` or `B` is an integer data type with the other a scalar `double`, then `C` assumes the integer data type.

• If either `A` or `B` is a table or timetable, then `C` is a table or timetable.

## Version History

Introduced before R2006a

expand all