Written and maintained by CASRAI Editorial Board
Published
On this page
Quick answer: M = max(A) returns the largest element of a vector, or the largest element of each column of a matrix. Add [M,I] = max(A) to also get the position of each maximum, max(A,[],"all") for one maximum over the whole array, and max(A,B) to compare two arrays element by element. This page maps each goal to the right call, then explains the NaN rule, the tie rule and the empty-bracket placeholder that cause most wrong results.
What max is used for in research code
Analysts reach for max to find a peak response in a signal, the largest residual in a fit, the strongest value in each experimental condition (one column per condition) or the winning class in a score matrix. The question that decides the call is not “what is the maximum?” but “maximum over which part of the array, and do I need to know where it is?” The decision table below answers that first.
Which max call fits your goal
| Goal | Call | What you get |
|---|---|---|
| Largest value in a vector | M = max(A) |
Single value, for example max([23 42 37 18 52]) returns 52 |
| Largest value in each column of a matrix | max(A) or max(A,[],1) |
Row vector of column maxima |
| Largest value in each row | max(A,[],2) |
Column vector of row maxima |
| One maximum over the entire array | max(A,[],"all") |
Single value |
| Maximum over several dimensions at once | max(A,[],[1 2]) |
For a 2-by-3-by-3 array, a 1-by-1-by-3 result |
| Value and position | [M,I] = max(A) |
Maxima plus index along the operating dimension |
| Value and position as a linear index into A | [M,I] = max(A,[],2,"linear") |
Maxima plus linear indices of A |
| Element-by-element larger of two arrays | C = max(A,B) |
Array of pairwise maxima, with implicit expansion |
| Treat NaN as a result instead of skipping it | max(A,[],"includemissing") |
NaN if the operating dimension contains one |
| Every position that reaches a threshold or the maximum | find(...) on a logical test |
Indices of all matches, see below |
| Maximum of a mathematical function on an interval | fminbnd on the negated function |
Location of a local maximum, see below |
Source for the syntaxes and results: MathWorks reference page for max (read 2026-11-08).
Access: where max is documented
max is a MATLAB function documented in the MATLAB reference at mathworks.com/help/matlab/ref/double.max.html. You run it in a MATLAB session or script, the same way as other array functions covered in our guides to loops in MATLAB and conditional statements in MATLAB. If you do not use MATLAB, the free alternatives table later on this page shows the equivalent calls in GNU Octave and NumPy.
Step by step: the max calls in order
1. Maximum of a vector (max in MATLAB, max function in MATLAB)
- Create the vector:
A = [23 42 37 18 52]; - Call
M = max(A). The result is 52.
2. Maximum of each column or row of a matrix
- Create the matrix:
A = [2 8 4; 7 3 9]; max(A)returns the column maxima7 8 9. For a matrix,max(A)andmax(A,[],1)are the same operation.max(A,[],2)returns the row maxima instead.
3. Maximum of the whole array
Use max(A,[],"all"). For an array of three or more dimensions, max(A,[],[1 2]) reduces only the first two dimensions: with a 2-by-3-by-3 array it returns a 1-by-1-by-3 result.
4. Maximum with its index (find max in MATLAB)
- Create
A = [1 9 -2; 8 4 -5]; - Call
[M,I] = max(A). - Read
M = 8 9 -2andI = 2 1 1: the 8 is in row 2 of column 1, the 9 in row 1 of column 2, the -2 in row 1 of column 3.
With the "linear" option the index refers to positions in the whole array instead. For A = [1 2 3; 4 5 6], the call [M,I] = max(A,[],2,"linear") returns M = [3;6] and I = [5;6]: MATLAB counts down each column first, so the 3 sits at linear position 5 and the 6 at position 6.
5. Larger of two arrays, element by element (MATLAB max between two values)
max(A,B) compares the two inputs element by element and uses implicit expansion, so a scalar can be one of them. With A = [1 7 3; 6 2 9] and B = 5, the result is [5 7 5; 6 5 9]. This is a clipping-from-below pattern: every value under 5 is lifted to 5.
6. All positions that match a condition (MATLAB find max, find max in MATLAB)
max reports the first occurrence only. To list every position that reaches a value, combine a logical test with find. find(X) returns the indices of nonzero elements, find(X,n) limits the output to the first n, and find(X,n,"last") takes the last n (the default direction is "first"). With X = magic(4), k = find(X<10,5) returns [2;3;4;5;7] and X(k) returns [5;9;4;2;7]. To find all tied maxima use find(A == max(A,[],"all")). Source: MathWorks reference page for find (read 2026-11-08).
7. Maximum of a function on an interval (Fminbnd in MATLAB)
max works on arrays of values, not on a function handle. The MathWorks fminbnd page describes a solver that finds a local minimum of a scalar function on a fixed interval, called as x = fminbnd(fun,x1,x2) or [x,fval,exitflag,output] = fminbnd(___). The documentation does not describe a maximizing option. The standard workaround, which is general practice rather than a statement from that page, is to minimize the negated function and negate the value back. For example, [x,fval] = fminbnd(@(x) -sin(x), 0, pi) searches for the peak of sin(x) on 0 to π; the location is the peak position and -fval is the maximum value. Because the solver returns a local result, check the answer against a plot or a coarse grid before reporting it.
Reading the output
NaN values: skipped by default
The MathWorks page states that by default max omits missing values. If you pass "includemissing" (or "includenan" for double, single and duration inputs), the result is NaN whenever any element along the operating dimension is missing. "omitnan" is the older, type-specific spelling of the default behaviour. Decide this deliberately: a silent skip can hide a failed measurement, and the other tools in the alternatives table below treat NaN differently.
Ties: the first occurrence wins
If the largest element occurs more than once, I holds the index of the first occurrence. Use the find pattern above when you need all of them.
Complex numbers
By default ("auto") complex values are compared by magnitude, with ties broken by phase angle. max([-1 2 -9],[],"ComparisonMethod","abs") returns -9, because -9 has the largest absolute value.
Troubleshooting
- “I wanted the row maxima and got column maxima.”
max(A)works down the columns of a matrix. Usemax(A,[],2)for rows. - “max(A,2) did not return the maximum along dimension 2.” In
max(A,B)the second argument is a second array. A dimension goes in the third position, after an empty-bracket placeholder:max(A,[],2). The[]is what tells MATLAB you are not supplying a second array. - “The index I does not match the position in my matrix.” Without
"linear",Iis an index along the operating dimension only, so for column maxima it is a row number. Add"linear"to index into the whole array. - “My result ignored a bad measurement.” The default skips NaN. Use
"includemissing"to make NaN visible. - “I need the location where a function peaks, not an array maximum.” See step 7; evaluate the function on a grid and call
maxon the values, or minimize the negated function withfminbnd.
Free alternatives to MATLAB max
Equivalent calls exist in two freely available environments. The rows below come from each project’s own documentation (read 2026-11-08); the differences in NaN handling are the main trap when you move code between tools.
| Behaviour | MATLAB | GNU Octave | NumPy |
|---|---|---|---|
| Basic call | max(A) |
m = max(x) |
numpy.max(a, axis=None, ...) |
| Choose dimension | max(A,[],dim) |
max(x,[],dim) |
axis= (an int or a tuple); by default the flattened input is used |
| Position of the maximum | [M,I] = max(A) |
[m,im] = max(...), returns the first index |
Separate function; numpy.argmax is listed under See also as returning the indices of the maximum values |
| Two arrays, element by element | max(A,B) |
max(x,y), following broadcasting rules |
Not covered by numpy.max itself |
| Default NaN handling | Omitted | Omitted ("omitnan" default; "includenan" returns NaN) |
Propagated: one NaN makes the result NaN; numpy.nanmax ignores NaN |
Sources: GNU Octave documentation, Utility Functions and NumPy reference for numpy.max.
FAQ
What does max do in MATLAB?
It returns the largest element of an array. For a vector that is one value; for a matrix it is by default one maximum per column; with "all" it is one value for the whole array.
How do I find the maximum value and its position in MATLAB?
Use two outputs: [M,I] = max(A). M holds the maxima and I the index of the first occurrence of each one, along the operating dimension unless you add "linear".
How do I find the maximum of a whole matrix in MATLAB?
Call max(A,[],"all").
How do I get the larger of two values or arrays?
Use max(A,B); it works element by element and expands a scalar automatically.
Does max ignore NaN?
Yes, by default. Pass "includemissing" if a NaN should make the result NaN.
How do I find every position of the maximum rather than only the first?
Combine a logical test with find: find(A == max(A,[],"all")).
How do I find the maximum of a function with fminbnd?
Minimize the negated function and negate the returned value. The fminbnd page describes a local minimum search on a fixed interval only.
Related reading: solving equations in MATLAB, the exponential function in MATLAB and matrix inversion in MATLAB.
Sources
- MathWorks, max (MATLAB documentation), read 2026-11-08.
- MathWorks, find (MATLAB documentation), read 2026-11-08.
- MathWorks, fminbnd (MATLAB documentation), read 2026-11-08.
- GNU Octave, Utility Functions, read 2026-11-08.
- NumPy, numpy.max, read 2026-11-08.







