max
Largest element, along a dimension, or the larger of two arrays.
Category: Language Fundamentals > Data Types
Syntax
M = max(A)
Largest element of a vector, or of each column of a matrix.
[M, I] = max(A)
Also returns the index of the largest element; the first one when there are ties.
M = max(A, [], dim)
Largest along dimension dim.
M = max(A, [], 'all')
Largest of all elements.
M = max(A, B)
Element-wise larger of A and B, with implicit expansion.
Description
By default NaN values are ignored: max([1 NaN 3]) is 3. The result is NaN only when all values are NaN. The empty matrix gives []. For complex numbers the comparison uses the absolute value, and for ties the angle.
The two-array form never returns the index, and the result has the class of the integer operand when one is an integer, saturating where needed.
Input arguments
A, B- Numeric, logical or char arrays, or datetime or duration arrays.
dim- Dimension to work along.
'omitnan' | 'includenan'- Whether NaN values are skipped (the default) or make the result NaN.
Output arguments
M- The largest values.
I- Indices of the maxima along the working dimension.
Example
Maxima of a matrix
A = [1 5; 7 2];
max(A)
max(A, [], 2)
max(A, [], 'all')
[m, i] = max([3 9 2 9])
max([1 NaN 3])
max(4, [1 6 2])
Output
ans = 1×2
7 5
ans = 2×1
5
7
ans = 7
m = 9
i = 2
ans = 3
ans = 1×3
4 6 4