To find the two largest values of a row of an array in MATLAB, you can use the max
function along with sorting. The max
function returns the maximum value in an array, along with its index. By sorting the row in descending order, the first two values will be the largest. Here's an example code snippet:
main.m191 chars4 lines
The sort
function sorts the rows of the matrix A
in descending order ('descend'
) along the second dimension (2
), which corresponds to the columns. This gives us a new matrix sortedA
where the first column contains the largest value of each row, and the second column contains the second largest value of each row.
We can then extract these top two values using the (:,1:2)
indexing, which selects all rows and the first two columns. The resulting variable topTwoValues
will be a matrix with two columns, containing the top two values of each row of A
.
gistlibby LogSnag