max - Maximum elements of array - MATLAB (original) (raw)
Maximum elements of array
Syntax
Description
[M](#bubhhu5-1-M) = max([A](#bubhhu5-1-A))
returns the maximum elements of an array.
- If
A
is a vector, thenmax(A)
returns the maximum ofA
. - If
A
is a matrix, thenmax(A)
is a row vector containing the maximum value of each column ofA
. - If
A
is a multidimensional array, thenmax(A)
operates along the first dimension ofA
whose size does not equal 1, treating the elements as vectors. The size ofM
in this dimension becomes1
, while the sizes of all other dimensions remain the same as inA
. IfA
is an empty array whose first dimension has zero length, thenM
is an empty array with the same size asA
. - If
A
is a table or timetable, thenmax(A)
returns a one-row table containing the maximum of each variable. (since R2023a)
[M](#bubhhu5-1-M) = max([A](#bubhhu5-1-A),[],`"all"`)
finds the maximum over all elements of A
.
[M](#bubhhu5-1-M) = max([A](#bubhhu5-1-A),[],[dim](#bubhhu5-1-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.
[M](#bubhhu5-1-M) = max([A](#bubhhu5-1-A),[],[vecdim](#mw%5F96d1b204-1097-4b98-bd36-1dffa686a8bc))
returns the maximum over the dimensions specified in the vectorvecdim
. For example, if A
is a matrix, then max(A,[],[1 2])
returns the maximum over all elements inA
because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.
[M](#bubhhu5-1-M) = max([A](#bubhhu5-1-A),[],___,[missingflag](#mw%5Fa2d64e05-e3cf-4321-96d8-f48e3c561e21))
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.
[[M](#bubhhu5-1-M),[I](#bubhhu5-1-I)] = max(___)
also returns the index into the operating dimension that corresponds to the first occurrence of the maximum value ofA
.
[[M](#bubhhu5-1-M),[I](#bubhhu5-1-I)] = max([A](#bubhhu5-1-A),[],___,"linear")
also returns the linear index into A
that corresponds to the maximum value inA
.
[C](#bubhhu5-1-C) = max([A](#bubhhu5-1-A),[B](#bubhhu5-1-B))
returns an array with the largest elements taken from A
orB
.
[C](#bubhhu5-1-C) = max([A](#bubhhu5-1-A),[B](#bubhhu5-1-B),[missingflag](#mw%5Fa2d64e05-e3cf-4321-96d8-f48e3c561e21))
also specifies how to treat missing values.
___ = max(___,"ComparisonMethod",[method](#mw%5F449936e6-bb36-4c6f-a6e7-d7e6b951485b))
optionally specifies how to compare elements for any of the previous syntaxes. For example, for a vector A = [-1 2 -9]
, the syntaxmax(A,[],"ComparisonMethod","abs")
compares the elements of A
according to their absolute values and returns a maximum value of -9
.
Examples
Create a vector and compute its largest element.
A = [23 42 37 18 52]; M = max(A)
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)
Create a matrix and compute the largest element in each column.
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
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.
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.
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)
.
[M,I] = max(A,[],2,"linear")
Create a matrix and return the largest value between each of its elements compared to a scalar.
Input Arguments
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
- If
A
is complex, thenmax(A)
returns the complex number with the largest magnitude. If magnitudes are equal, thenmax(A)
returns the value with the largest magnitude and the largest phase angle. - If
A
is a scalar, thenmax(A)
returnsA
. - If
A
is a 0-by-0 empty array, thenmax(A)
is as well.
If A
has type categorical
, then it must be ordinal.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| categorical
| datetime
| duration
| table
| timetable
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 whose size does not equal 1.
Dimension dim
indicates the dimension whose length reduces to 1
. The size(M,dim)
is1
, while the sizes of all other dimensions remain the same, unless size(A,dim)
is 0
. Ifsize(A,dim)
is 0
, thenmax(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 ofA
and returns a1
-by-n
row vector.max(A,[],2)
computes the maximum of the elements in each row ofA
and returns anm
-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
. Thenmax(A,[],[1 2])
returns a 1-by-1-by-3 array whose elements are the maximums computed over each page ofA
.
Additional input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. Inputs A
andB
must either be the same size or have sizes that are compatible (for example, A
is anM
-by-N
matrix andB
is a scalar or1
-by-N
row vector). For more information, see Compatible Array Sizes for Basic Operations.
- If
A
andB
are both arrays, then they must be the same data type unless one is adouble
. In that case, the data type of the other array can besingle
,duration
, or any integer type. - If
A
andB
are ordinalcategorical
arrays, they must have the same sets of categories with the same order. - If either
A
orB
is a table or timetable, then the other input can be an array, table, or timetable.
If B
has type categorical
, then it must be ordinal.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| categorical
| datetime
| duration
| table
| timetable
Complex Number Support: Yes
Missing value condition, specified as one of the values in this table.
Value | Input Data Type | Description |
---|---|---|
"omitmissing" | All supported data types | Ignore 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 arrayA
, compare elements byreal(A)
whenA
is real, and byabs(A)
whenA
is complex."real"
— For a numeric input arrayA
, compare elements byreal(A)
whenA
is real or complex. IfA
has elements with equal real parts, then useimag(A)
to break ties."abs"
— For a numeric input arrayA
, compare elements byabs(A)
whenA
is real or complex. IfA
has elements with equal magnitude, then useangle(A)
in the interval (-π,π] to break ties.
Output Arguments
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)
is0
. If size(A,dim)
is0
, 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 ofA
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
andB
. For more information, see Compatible Array Sizes for Basic Operations.
The data type of C
depends on the data types ofA
and B
:
- If
A
andB
are the same data type, thenC
matches the data type ofA
andB
. - If either
A
orB
issingle
, thenC
issingle
. - If either
A
orB
is an integer data type with the other a scalardouble
, thenC
assumes the integer data type. - If either
A
orB
is a table or timetable, thenC
is a table or timetable.
Extended Capabilities
Themax
function supports tall arrays with the following usage notes and limitations:
- Index output is not supported for tall tabular inputs.
For more information, see Tall Arrays.
Usage notes and limitations:
- To use an empty array as the second argument at run time, you must specify a fixed-size, 0-by-0 array at code generation time.
- If used, the arguments
dim
,vecdim
,missingflag
, andmethod
must be constant at code generation time. - For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- In the generated code, the input array remains complex, even if all of its elements have zero-valued imaginary parts. Under these circumstances, the results produced by the generated code might differ from those produced by MATLAB. See Code Generation for Complex Data with Zero-Valued Imaginary Parts (MATLAB Coder).
Usage notes and limitations:
- To use an empty array as the second argument at run time, you must specify a fixed-size, 0-by-0 array at code generation time.
- If used, the arguments
dim
,vecdim
,missingflag
, andmethod
must be constant at code generation time. - If you do not specify the
dim
argument, the code generator operates along the first dimension ofA
that is variable-size or whose size does not equal 1. - If
A
is complex, and all of the elements ofA
have zero-valued imaginary parts, then MATLAB® might convertA
toreal(A)
before callingmax(A)
. In this case, MATLAB compares elements byreal(A)
, but the generated code compares elements byabs(A)
. To make the generated code match MATLAB, usemax(real(A))
ormax(A,'ComparisonMethod','real')
. See Code Generation for Complex Data with Zero-Valued Imaginary Parts (MATLAB Coder).
Usage notes and limitations:
- Inputs of 3-D matrices or greater are not supported.
- Inputs that have complex data types are not supported.
- Input matrices or vectors must be of equal size.
The max
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Specifying a second input array as a character array returns an error. This change minimizes confusion with other arguments that can be specified as character vectors, such as the missing value condition. To maintain the previous functionality, you can convert the second input array to double
, for example,max(A,double(B),"includenan")
.
Specifying a second input array as a character array gives a warning and will generate an error in a future release. This change minimizes confusion with other arguments that can be specified as character vectors, such as the missing value condition. To maintain the previous functionality, you can convert the second input array to double
, for example,max(A,double(B),"includenan")
.
Omit or include all missing values in the input arrays when computing the maximum value by using the "omitmissing"
or"includemissing"
options. Previously,"omitnan"
, "includenan"
,"omitnat"
, "includenat"
,"omitundefined"
, and "includeundefined"
specified a missing value condition that was specific to the data type of the input arrays.
The max
function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
Specify the real or absolute value method for determining the maximum value of the input by using the ComparisonMethod
parameter.
Operate on multiple dimensions of the input arrays at a time. Specify a vector of operating dimensions, or specify the "all"
option to operate on all array dimensions.