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:
Other People Are Reading
Type the following command to determine the maximum value of a one-dimensional array (vector) called "x:"
Type the following command to determine the maximum value in each column of a 2-D array (matrix):
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:
Nest "max" with itself to get the maximum value in a matrix:
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.
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.