max() - Signal Processing
exampleM = max(A) returns
the largest elements of A.If A is a vector, then max(A) returns
the largest element of A.If A is a matrix, then max(A) is
a row vector containing the maximum value of each column.If A is a multidimensional array,
then max(A) operates along the first array dimension
whose size does not equal 1, treating the elements
as vectors. The size of this dimension becomes 1 while
the sizes of all other dimensions remain the same. If A is
an empty array with first dimension 0, then max(A) returns
an empty array with the same size as A.
exampleM = max(A,[],dim) returns
the largest elements along dimension dim. For example,
if A is a matrix, then max(A,[],2) is
a column vector containing the maximum value of each row.
example[M,I] =
max(___) finds the indices of the maximum values
of A and returns them in output vector I,
using any of the input arguments in the previous syntaxes. If the
maximum value occurs more than once, then max returns
the index corresponding to the first occurrence.
exampleC = max(A,B) returns
an array with the largest elements taken from A or B.
example___ = max(___,nanflag) specifies
whether to include or omit NaN values in the calculation
for any of the previous syntaxes. For the single input case, to specify nanflag without
specifying dim, use max(A,[],nanflag).
For example, max(A,[],'includenan') includes all NaN values
in A while max(A,[],'omitnan') ignores
them.
Syntax
M = max(A) exampleM = max(A,[],dim) example[M,I] =
max(___) exampleC = max(A,B) example___ = max(___,nanflag) example
Example
Largest Vector ElementOpen This ExampleCreate a vector and compute its largest element.A = [23 42 37 18 52];
M = max(A)
M =
52
Largest Complex ElementOpen This ExampleCreate 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
Largest Element in Each Matrix ColumnOpen This ExampleCreate a matrix and compute the largest element in each column.A = [2 8 4; 7 3 9]
A =
2 8 4
7 3 9
M = max(A)
M =
7 8 9
Largest Element in Each Matrix RowOpen This ExampleCreate a matrix and compute the largest element in each row.A = [1.7 1.2 1.5; 1.3 1.6 1.99]
A =
1.7000 1.2000 1.5000
1.3000 1.6000 1.9900
M = max(A,[],2)
M =
1.7000
1.9900
Largest Element IndicesOpen This ExampleCreate 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 =
1 9 -2
8 4 -5
[M,I] = max(A)
M =
8 9 -2
I =
2 1 1
Largest Element ComparisonOpen This ExampleCreate a matrix and return the largest value between each of its elements compared to a scalar.A = [1 7 3; 6 2 9]
A =
1 7 3
6 2 9
B = 5;
C = max(A,B)
C =
5 7 5
6 5 9
Largest Element in MatrixOpen This ExampleCreate a matrix A and use its column representation, A(:), to find the value and index of the largest element.A = [8 2 4; 7 3 9]
A =
8 2 4
7 3 9
A(:)
ans =
8
7
2
3
4
9
[M,I] = max(A(:))
M =
9
I =
6
I is the index of A(:) containing the largest element.Now, use the ind2sub function to extract the row and column indices of A corresponding to the largest element.[I_row, I_col] = ind2sub(size(A),I)
I_row =
2
I_col =
3
If you need only the maximum value of A and not its index, then call the max function twice.M = max(max(A))
M =
9
Largest Element Involving NaNOpen This ExampleCreate a vector and compute its maximum, excluding NaN values.A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
M = max(A,[],'omitnan')
M =
3.9800
max(A) will also produce this result since 'omitnan' is the default option.Use the 'includenan' flag to return NaN.M = max(A,[],'includenan')
M =
NaN
Output / Return Value
Limitations
Alternatives / See Also
Reference