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:
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.
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:
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:
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:
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:
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:
[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:
The following code will find the maximum value from 4×4 elements.
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.