Find n minimum values ​​in an array

I am using Matlab 2012a.

I have an array of k cells (say 1000). I need to find the 5 smallest values ​​of this array and execute the average of these values ​​in X and Y.

Does anyone have an idea how to do this?

+6
source share
3 answers

Assuming you have arrays of X and Y and you want to find the five smallest values ​​of Y:

[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 mi(1:5) indices mi(1:5) correspond to the five lowest values. Taking mean these values ​​for X and Y will do what we want. If I do not understand your expression about the problem, clarify your question and I will take another picture.

+13
source

How to sort your array from the lowest value to the highest, and then select the first 5 values. These will be the 5 minute values ​​of your array. Then do the average of these 5 values.

This may not be the most efficient way to work with memory, but in just 1000 values ​​it will do its job!

Hope this helps!

+1
source

use minmaxselection MATLAB MEX package 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 
+1
source

All Articles