Find n minimum values in an array

混江龙づ霸主 提交于 2019-11-28 12:20:52

Assuming you have arrays X and Y, and you want to find the five lowest Y values:

[m mi] = sort(Y);
lowest5index = mi(1:5);
lowest5Y = Y(lowest5index);
lowest5X = X(lowest5index);

meanYlowest5 = mean(lowest5Y);
meanXlowest5 = mean(lowest5X);

Explanation:

The sort command with two output parameters returns both the sorted array (in m) and the indices in the original array (mi). The first five indices mi(1:5) correspond to the five lowest values. Taking the mean of these values for both X and Y will do what we want. If I didn't understand your problem statement, please clarify your question and I will take another shot at it.

How about doing a sort of your array from lowest value to the highest and then selecting the 5 first values. Those will be the 5 min values of your array. Then perform a mean of those 5 values.

This might not be the most memory efficient way of doing this but for just 1000 values it will get the job done!

Hope it helps!

use minmaxselection MATLAB MEX package, which has been specially optimized for this problem:

a = [2,3,4,7,56,4,21, 64, -2];
mink(a, 2)

<< ans = 
<<    -2  2    

mink(a,4)

<< ans =
<<    -2     2     3     4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!