查找最大元素?

7次观看(最近30天)
大家好
I have an array say A = [1 5 7 4 8 10 14]. I want to find most largest, second most largest and 3rd most largest elements from A.
我已经发现可以使用MAXK函数来完成它,该功能可用于MATLAB的更新版本,但我正在使用R2017A,其中Maxk函数无法使用。
无论如何,我想像ANS = [14 10 8]看到输出。请提前帮助。

接受的答案

马特·J
马特·J 2021年8月13日
编辑:马特·J 2021年8月13日
有效的方法可能是使用文件交换MEX替代品 maxk() 像这样,
但是你也可以做
a = [1 5 7 4 8 10 14];
b = stort(a,“下降”);
B = B(1:3)
b = 1×3
14 10 8
2条评论
马特·J
马特·J 2021年8月13日
我不建议这样做。
一个=兰德(3 e7, 1);
tic;
b = maxk(a,3);
TOC
经过的时间为0.039257秒。
tic;
b = stort(a,“下降”);
b = b(1:3);
TOC;
经过的时间为1.968519秒。
tic;
[〜,idx] = sort(a);
b = a(idx(end:end-2));
TOC
经过的时间为2.338227秒。

登录发表评论。

更多答案(0)

标签

s manbetx 845


发布

R2017A

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

开始狩猎!