Matlab

max() Function in MATLAB

MATLAB is a popular programming environment with various built-in features to perform mathematical operations. One of the most widely used fundamental functions is the max() function of MATLAB, used to find the maximum value in an array or a specified matrix. The max() function has different modes for processing input and output data.

In this guide, we will discuss the usage of the max() function with some examples to illustrate the working of this function on arrays and matrices in MATLAB.

What is max() Function in MATLAB

The max() function in MATLAB is used to determine the highest value in an array. The basic syntax for using the max() function is:

result = max(data)

 

The max() function returns the maximum value from the specified data.

How to Use the max() Function in MATLAB?

In this section, you will find various example programs of using the max() function in MATLAB.

1: Find the Maximum Value in an Array Using MATLAB max() Function

In MATLAB, the max() function is used to determine the highest value from an array. The below code snippet uses the max() function to find the maximum value from the data array.

data = [23,16,98,18,27,9,0]
r = max(data)

 

2: Find the Maximum Value in Matrix Using MATLAB max() Function

In MATLAB, the max() function has several ways to process the data and has different input parameters. In the following example, we have a matrix of 6×6 elements, and we are finding the largest value of each column of the matrix. The max() function finds the largest value of the column by default:

data = magic(6)
r = max(data)

 

In the above program, we have determined the maximum element of each column. To find the maximum value of the row, you can use the dim parameter with the max() function. The syntax of using the max() function for finding the maximum value of a row is shown below:

r = max(a, [ ], dim)

 

The dim option has different values, you can use according to the need:

 

dim value

Purpose

1 Finds the maximum element from each column
2 Finds the maximum value from each row

 

In the below sample code, we are finding the maximum value of the row of the 6×6 matrix:

data = magic(6)
r = max(data, [ ], 2)

 

3: Find the Index of the Maximum Value in a Matrix Using MATLAB max() Function

If you want to find the maximum value from an array with an index, you can use the following syntax:

[r, i] = max(a)

 

The following code finds the index of the element with the largest value. The r will return the largest value of each column of the matrix, and i will return the index position of the maximum element:

data = magic(6)
[r, i] = max(data)

 

4: MATLAB max() Function with “all” Parameter

If you want to find the largest value from a given matrix, you can use all” parameter, whose syntax is given below:

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

 

The following code will find the maximum value from 4×4 elements.

data = magic(4)
r = max(data, [], 'all')

 

Bottom Line

MATLAB provides the max() function used to find the maximum value in an array or matrix. It has different modes for processing data and can be helpful in finding the maximum value in a variety of scenarios. In this tutorial, we have learned the usage of the max() function with examples. By understanding the max() function effectively, MATLAB users can easily analyze and manipulate their data to extract the desired information.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.