Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select numbers from array which are much greater than the rest

Say there is an array of n elements, and out of n elements there be some numbers which are much bigger than the rest.

So, I might have:

16, 1, 1, 0, 5, 0, 32, 6, 54, 1, 2, 5, 3

In this case, I'd be interested in 32, 16 and 54.

Or, I might have:

32, 105, 26, 5, 1, 82, 906, 58, 22, 88, 967, 1024, 1055 

In this case, I'd be interested in 1024, 906, 967 and 1055.

I'm trying to write a function to extract the numbers of interest. The problem is that I can't define a threshold to determine what's "much greater", and I can't just tell it to get the x biggest numbers because both of these will vary depending on what the function is called against.

I'm a little stuck. Does anyone have any ideas how to attack this?

like image 989
NOP Avatar asked Dec 13 '25 15:12

NOP


1 Answers

Just taking all the numbers larger than the mean doesn't cut it all the time. For example if you only have one number which is much larger, but much more numbers wich are close to each other. The one large number won't shift the mean very much, which results in taking too many numbers:

data = [ones(1,10) 2*ones(1,10) 10];
data(data>mean(data))

ans =

      2     2     2     2     2     2     2     2     2     2    10

If you look at the differences between numbers, this problem is solved:

>> data = [16, 1, 1, 0, 5, 0, 32, 6, 54, 1, 2, 5, 3];
sorted_data = sort(data);
dd = diff(sorted_data);
mean_dd = mean(dd);
ii = find(dd> 2*mean_dd,1,'first');
large_numbers = sorted_data(ii:end);

large_numbers =

   6    16    32    54

the threshold value (2 in this case) lets you play with the meaning of "how much greater" a number has to be.

like image 125
Gunther Struyf Avatar answered Dec 15 '25 10:12

Gunther Struyf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!