# max

Largest elements

## Description

example

C = max(A) returns the largest element of A if A is a vector. If A is a matrix, this syntax treats the columns of A as vectors, returning a row vector containing the largest element from each column.

example

C = max(A,[],dim) returns the largest elements of matrix A along the dimension dim. Thus, max(A,[],1) returns a row vector containing the largest elements of each column of A, and max(A,[],2) returns a column vector containing the largest elements of each row of A.

Here, the required argument [] serves as a divider. If you omit it, max(A,dim) compares elements of A with the value dim.

example

[C,I] = max(___) finds the indices of the largest elements, and returns them in output vector I. If there are several identical largest values, this syntax returns the index of the first largest element that it finds.

example

C = max(A,B) compares each element of A with the corresponding element of B and returns C containing the largest elements of each pair.

## Examples

### Maximum of Vector of Numbers

Find the largest of these numbers. Because these numbers are not symbolic objects, you get a floating-point result.

max([-pi, pi/2, 1, 1/3])
ans =
1.5708

Find the largest of the same numbers converted to symbolic objects.

max(sym([-pi, pi/2, 1, 1/3]))
ans =
pi/2

### Maximum of Each Column in Symbolic Matrix

Create matrix A containing symbolic numbers, and call max for this matrix. By default, max returns the row vector containing the largest elements of each column.

A = sym([0, 1, 2; 3, 4, 5; 1, 2, 3])
max(A)
A =
[ 0, 1, 2]
[ 3, 4, 5]
[ 1, 2, 3]

ans =
[ 3, 4, 5]

### Maximum of Each Row in Symbolic Matrix

Create matrix A containing symbolic numbers, and find the largest elements of each row of the matrix. In this case, max returns the result as a column vector.

A = sym([0, 1, 2; 3, 4, 5; 1, 2, 3])
max(A,[],2)
A =
[ 0, 1, 2]
[ 3, 4, 5]
[ 1, 2, 3]

ans =
2
5
3

### Indices of Largest Elements

Create matrix A. Find the largest element in each column and its index.

A = 1./sym(magic(3))
[Cc,Ic] = max(A)
A =
[ 1/8,   1, 1/6]
[ 1/3, 1/5, 1/7]
[ 1/4, 1/9, 1/2]

Cc =
[ 1/3, 1, 1/2]

Ic =
2     1     3

Now, find the largest element in each row and its index.

[Cr,Ir] = max(A,[],2)
Cr =
1
1/3
1/2

Ir =
2
1
3

If dim exceeds the number of dimensions of A, then the syntax [C,I] = max(A,[],dim) returns C = A and I = ones(size(A)).

[C,I] = max(A,[],3)
C =
[ 1/8,   1, 1/6]
[ 1/3, 1/5, 1/7]
[ 1/4, 1/9, 1/2]

I =
1     1     1
1     1     1
1     1     1

### Largest Elements of Two Symbolic Matrices

Create matrices A and B containing symbolic numbers. Use max to compare each element of A with the corresponding element of B, and return the matrix containing the largest elements of each pair.

A = sym(pascal(3))
B = toeplitz(sym([pi/3 pi/2 pi]))
maxAB = max(A,B)
A =
[ 1, 1, 1]
[ 1, 2, 3]
[ 1, 3, 6]

B =
[ pi/3, pi/2,   pi]
[ pi/2, pi/3, pi/2]
[   pi, pi/2, pi/3]

maxAB =
[ pi/3, pi/2, pi]
[ pi/2,    2,  3]
[   pi,    3,  6]

### Maximum of Complex Numbers

When finding the maximum of these complex numbers, max chooses the number with the largest complex modulus.

modulus = abs([-1 - i, 1 + 1/2*i])
maximum = max(sym([1 - i, 1/2 + i]))
modulus =
1.4142    1.1180

maximum =
1 - 1i

If the numbers have the same complex modulus, min chooses the number with the largest phase angle.

modulus = abs([1 - 1/2*i, 1 + 1/2*i])
phaseAngle = angle([1 - 1/2*i, 1 + 1/2*i])
maximum = max(sym([1 - 1/2*i, 1/2 + i]))
modulus =
1.1180    1.1180

phaseAngle =
-0.4636    0.4636

maximum =
1/2 + 1i

## Input Arguments

collapse all

### A — Inputsymbolic number | symbolic vector | symbolic matrix

Input, specified as a symbolic number, vector, or matrix. All elements of A must be convertible to floating-point numbers. If A is a scalar, then max(A) returns A. A cannot be a multidimensional array.

### dim — Dimension to operate alongpositive integer

Dimension to operate along, specified as a positive integer. The default value is 1. If dim exceeds the number of dimensions of A, then max(A,[],dim) returns A, and [C,I] = max(A,[],dim) returns C = A and I = ones(size(A)).

### B — Inputsymbolic number | symbolic vector | symbolic matrix

Input, specified as a symbolic number, vector, or matrix. All elements of B must be convertible to floating-point numbers. If A and B are scalars, then max(A,B) returns the largest of A and B.

If one argument is a vector or matrix, the other argument must either be a scalar or have the same dimensions as the first one. If one argument is a scalar and the other argument is a vector or matrix, then max expands the scalar into a vector or a matrix of the same length with all elements equal to that scalar.

B cannot be a multidimensional array.

## Output Arguments

collapse all

### C — Largest elementssymbolic number | symbolic vector

Largest elements, returned as a symbolic number or vector of symbolic numbers.

### I — Indices of largest elementssymbolic number | symbolic vector | symbolic matrix

Indices of largest elements, returned as a symbolic number or vector of symbolic numbers. [C,I] = max(A,[],dim) also returns matrix I = ones(size(A)) if the value dim exceeds the number of dimensions of A.

expand all

### Tips

• Calling max for numbers (or vectors or matrices of numbers) that are not symbolic objects invokes the MATLAB® max function.

• For complex input A, max returns the complex number with the largest complex modulus (magnitude), computed with max(abs(A)). If complex numbers have the same modulus, max chooses the number with the largest phase angle, max(angle(A)).

• max ignores NaNs.