You can use the max()
function in MATLAB to find the maximum number in a row of a matrix. By default, this function returns the maximum value and its corresponding index along the first non-singleton dimension in the input matrix. You can specify the dimension to operate along using the optional argument.
Here's an example code snippet that finds the maximum number in the second row of a sample matrix:
main.m52 chars3 lines
In this code:
A
is a 3x3 matrix with values [1 2 3; 4 5 6; 7 8 9]
A(2,:)
max()
function to this row, which returns the maximum value in the row, i.e. 6
max_value
variableIf you want to find the maximum value in each row of a matrix, you can use the max()
function with the dim
parameter set to 2
, which will operate along the rows of the matrix. Here's an example:
main.m53 chars3 lines
In this code:
A
is the same 3x3 matrix as beforemax()
function to A
with the dim
parameter set to 2
using the []
notation, which tells MATLAB to use its default behavior for the dim
parameter (i.e. operate along rows)[3; 6; 9]
containing the maximum value in each row of A
, in ordergistlibby LogSnag