# How to find the maximum in array matlab

Written by michael carroll
• Share
• Tweet
• Share
• Email

Matrices and vectors are the essential mathematical structures used in MATLAB. From a computational perspective, however, matrices and vectors are simply different types of arrays. An array is an ordered set of data, and each element of an array might be an array itself. A two-dimensional matrix, therefore, is an array of arrays. The "max" function is used to determine the maximum value in a MATLAB array or to find the maximum element in each row or column of a matrix.

Skill level:
Easy

## Instructions

1. 1

Type the following command to determine the maximum value of a one-dimensional array (vector) called "x:"

max(x)

2. 2

Type the following command to determine the maximum value in each column of a 2-D array (matrix):

max(x,[],1)

The result is returned as a vector. The third argument, "1," specifies that "max" operate along the 1st dimension (across the columns of the matrix). To get the maximum value in each row, use:

max(x,[],2)

3. 3

Nest "max" with itself to get the maximum value in a matrix:

max(max(x))

By default, max(x) is the same as max(x,[],1), so the result of the inner "max" command is a vector containing the maximum elements of each column. The second (outer) "max" function returns the maximum value from this vector, so you wind up with the maximum value of the whole matrix.

4. 4

Assign the results of "max" to two variables to get both the maximum value and its index in the array:

[z,i] = max(x)

After this command, "z" contains the maximum value (or vector of maximum values), and i contains its index (or vector of indices) in the array.

### Don't Miss

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent