Question

I am using Matlab 2012a.

I have an array of k cells (say 1000). I need to find the 5 lowest values of this array and need to do an average of those values in X and Y.

Anyone has an idea how to do that?

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top