【问题标题】:GNU Octave/Matlab: How to get the index of the three biggest values in an array?GNU Octave/Matlab:如何获取数组中三个最大值的索引?
【发布时间】:2021-04-04 14:45:05
【问题描述】:

假设我有一个数组A = [ 41 13 22 18 32] 如何获取三个最大值的索引?

在这个例子中,我希望得到 1,5,3(索引为 41,32,22)。

谢谢!

【问题讨论】:

    标签: arrays matlab indexing max


    【解决方案1】:

    根据this 文档:

    A = [ 41 13 22 18 32];
    [val idx]=sort(A,'descend');
    top3_idx=idx(1:3)
    

    它适用于Octave online

    【讨论】:

    • 理论上应该这样做,你是对的.. 但在 Octave 中似乎没有做任何事情......“'maxk' 函数尚未在 Octave 中实现。” ://
    • 这在 octave 在线工作:A = [ 41 13 22 18 32]; [x idx]=sort(A,'descend'); top3_idx=idx(1:3)
    • 其实 maxk 返回的是值,而不是索引,对不起,我编辑了答案
    • 谢谢!这行得通!同时,我发现了另一个可能令人费解的解决方案:A = [ 41 13 22 18 32] B = flipud(sortrows(reshape([ A ; (1:length(A)) ], 2, []).')) max1 = B(1,2) max2 = B(2,2) max3 = B(3,2)
    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 2016-03-31
    • 2013-08-10
    • 2011-10-18
    • 2013-07-11
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多