How can I find the maximum value and its index in array in MATLAB?

后端 未结 6 2033
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 06:54

Suppose I have an array, a = [2 5 4 7]. What is the function returning the maximum value and its index?

For example, in my case that function should ret

6条回答
  •  长情又很酷
    2020-11-29 07:34

    For a matrix you can use this:

    [M,I] = max(A(:))
    

    I is the index of A(:) containing the largest element.

    Now, use the ind2sub function to extract the row and column indices of A corresponding to the largest element.

    [I_row, I_col] = ind2sub(size(A),I)
    

    source: https://www.mathworks.com/help/matlab/ref/max.html

提交回复
热议问题