Get the indices of the n largest elements in a matrix

前端 未结 4 2053
情书的邮戳
情书的邮戳 2020-11-27 15:46

Suppose I have the following matrix:

01 02 03 06
03 05 07 02
13 10 11 12
32 01 08 03

And I want the indices of the top 5 elements (in this

4条回答
  •  醉酒成梦
    2020-11-27 16:43

    If you have a rather big array and only want a few elements out of it. This would be my solution.

    Arraycopy = Array;
    for j = 1:n
       [a, Index(j)] = max(Arraycopy);
       Arraycopy(Index(j)) = -inf;
    end
    maximumValues = Array(Index);
    

    I think it should be faster and less RAM demanding than the sort solution.

提交回复
热议问题