Matlab

max function in MatLab

In the following article, we explain how to use the max() function in MATLAB® to find the maximum value among the elements of an array. We then show you a complete description of the main features and various ways to use this function to process 2D and multidimensional arrays.

This article includes practical examples and images to understand better how the min() function works in MATLAB.

MATLAB max() function Syntax

r = max(a)

r = max(a, [ ], dim)
r = max(a, [ ], ‘all’)
r = max(a, [ ], nanflag)
r = max(a, [ ], dim, nanflag)
r = max(a, [ ], vecdim)
r = max(a, [ ], ‘all’, nanflag)
r = max(a, [ ], vecdim, nanflag)

r = max(a, b)
[r, i ] = max(___)
[r, i ] =max(a, [ ], ‘all’, ___)
[r, i ] =max(a, [ ], ___, ‘linear’)

MATLAB max() description and examples

The max() function returns in “r” the maximum value among all elements of the array “a”. This function can also return the index of the maximum values sent in “a” and returned in “r”.

max() has different modes for processing input and output data. When the function is called, these modes are selected by flags, size and dimension vectors.

The types of input arguments accepted by this function are scalars, vectors, matrices, and multidimensional arrays. The data types supported by max() are single, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, logical, categorical, datetime, and duration. We will show you the different modes this function has to get the maximum values in the following examples.

Below we will detail each of the arguments and input flags of the max() function:

a: This input specifies the input scalar, vector, or matrix. The type of data supported by this input is scalar, vector, matrix, or multidimensional array.

b: Additional input matrix. In cases where max() is sent with an additional array, it must have dimensions compatible with “a”. The data type for this input is the same as for “a.”

dim: This input sets the dimension on which the max() function will operate. The data type supported by “dim” is a positive integer scalar.

vecdim: dimension vector. max() returns the maximum value among the elements in the array dimensions specified in this vector.

nanflag: This flag specifies if NaN values should be included in the output array

i: index. In this output, max() returns the index of the maximum values obtained from “a”.

How to get the maximum value between the elements of a vector with the max() function of MATLAB.

This example shows how to determine the maximum value in a row vector. We create the vector “a” with elements of different values and send it as an input argument to the max() function.

a = [4,13,5,16,53,66,2,16,18,88,15,52,5];
r = max(a)

The max() function searches all values of the individual elements of the vector “a” and returns the largest value in “r”.

How to get the maximum element of each column with the max() function in MATLAB.

As explained above, the max() function has several ways to process input and output arguments. In this example, we will show you how to get the maximum value of each column of a 3×3 element magic square.

a = magic(3)
r = max(a)

The max() function returns the row vector “r” with the maximum value of each column.

How to get the maximum element of each row with the “dim” option of the max() function in MATLAB.

In the example above, the data in the matrix was processed column by column. The max() function handles input arguments this way by default. You can use the “dim” flag to specify the dimension from which max() gets the maximum values using the following syntax:

r = max(a, [ ], dim)

If the “dim” option is 1, the max() function returns a row vector “r” with the maximum value of each column. If the “dim” option is 2, max() returns a column vector “r” with the maximum value of each row, as shown below:

In the following example, we will show you how to find the maximum value of each row in a 3×3 magic square that we will create.

a = magic(3)
r = max(a, [ ], 2)

As a result, the max() function returns the column vector “r” with the maximum value of each row of the magic square.

How to get the index of the maximum values with the MATLAB function max().

With max(), it is possible to get the location index of each maximum value of “a” and “r”. This function returns a vector of the same type and size as “r” with the index of the maximum values of each row or column. The syntax is as follows.

[r, i] = max(a)

a = magic(5)
[r, i] = max(a)

How to get the maximum single value of an array using the “all” option of the max() function in MATLAB.

Using the “all” option of the max() function, we can obtain the maximum unique value among all the elements of the array. Let us see the syntax for this mode below.

r = max(a, [ ],’all’)

The following example shows how to get the maximum unique value from an array with 5×5 elements.

a= [ 7, 12, 29, 56, 11;
    55, 22, 18, 68, 48;
    45, 18, 66, 97, 47;
    29, 51, 32, 44, 71]

r = max(a, [], 'all')

If you specify the flag ‘all’ in the function call, max() returns the maximum single value of all values of “a”, as shown in the figure.

How to include or exclude NaN values in the output arguments of the max() function in MATLAB

The max function has the option to include or exclude NaN values in the output arguments. This is done via the flags “includenan” and “‘omitnan'”. By default, max() excludes NaN values. Below we look at the syntax for this mode.

r= max(A,[ ],’omitnan’)

In the first example, these values were included in the output array via the “includenan” flag, while in the second example, they were omitted via the “omitnan” flag.

%Example 1

a= [1, 26, 78, 67, NaN; 11, 63, 14, NaN, NaN];
r= max(a,[], 2, 'includenan')

%Example 2

a= [1, 26, 78, 67, NaN; 11, 63, 14, NaN, NaN];
r= max(a,[], 2, 'omitnan')

The result for each example is shown below.

Conclusion

In this article, we explained how to use the MATLAB max() function to obtain the maximum values of an array, matrix, or vector. We also explained the various options that this function provides when handling input and output data. In this article, we have given some practical examples using different arrays and data types. We have also explained the input argument types of this function and the type of data accepted in more detail. We hope that this MATLAB article was useful for you. See other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).