Finding indexes of maximum values of an array

后端 未结 3 1357
鱼传尺愫
鱼传尺愫 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:26

    To get the indices of the two largest elements: use the second output of sort to get the sorted indices, and then pick the last two:

    x = [3 2 5 4 7 3 2 6 4];
    [~, ind] = sort(x);
    result = ind(end-1:end);
    

    In this case,

    result =
         8     5
    

提交回复
热议问题