Finding indexes of maximum values of an array

后端 未结 3 1388
鱼传尺愫
鱼传尺愫 2021-01-22 11:00

How do I find the index of the 2 maximum values of a 1D array in MATLAB? Mine is an array with a list of different scores, and I want to print the 2 highest scores.

3条回答
  •  长发绾君心
    2021-01-22 11:24

    You can use sort, as @LuisMendo suggested:

    [B,I] = sort(array,'descend');
    

    This gives you the sorted version of your array in the variable B and the indexes of the original position in I sorted from highest to lowest. Thus, B(1:2) gives you the highest two values and I(1:2) gives you their indices in your array.

提交回复
热议问题