找到一个阵列的最大值的索引 [英] Finding indexes of maximum values of an array
本文介绍了找到一个阵列的最大值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要如何找到在MATLAB一维数组的2最大值的指数?我的是用不同的分数列表的数组,我要打印2得分最高。
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.
推荐答案
我会去一个 O(K * N)
解决方案,其中 K
是你要找的,而不是为O(n log n)的
最高值的数目:
I'll go for an O(k*n)
solution, where k
is the number of maximum values you're looking for, rather than O(n log n)
:
x = [3 2 5 4 7 3 2 6 4];
y = x; %// make a copy of x because we're going to modify it
[~, m(1)] = max(y);
y(m(1)) = -Inf;
[~, m(2)] = max(y);
m =
5 8
这是唯一可行的,如果 K
小于日志N
。事实上,如果 K> = 3
我把它放在一个循环,这可能会得罪一些情面。 ;)
This is only practical if k
is less than log n
. In fact, if k>=3
I would put it in a loops, which may offend the sensibilities of some. ;)
这篇关于找到一个阵列的最大值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文