Search code examples
matlabsignal-processingeigenvalue

How to find and label the most frequent values with a tiny variance in MATLAB?


I have a vector d:

d = [
1.19011941712580e-06
6.39136179286748e-06
1.26442316296575e-05
1.81039120389278e-05
1.91304903300688e-05
2.19912290910362e-05
2.94113112667430e-05
3.42238417249065e-05
4.14201181268186e-05
5.76014376298924e-05
6.81337071520188e-05
0.000108396864465101
0.000130922201344182
0.000145712942644687
0.000174386494384153
0.000262758083529471
03050975943883
0.000373066486719321
0.000423949134658855
0.000489079623696380
0.000548432526451254
0.000694787830192734
0.000881370593483890
0.00125516689720339
0.00145237435686831
0.00815957230852142
0.0210146005799470
0.0507995676939279
0.0541594307796186
1
]

Plotting d:

plot(d, 'x:')

enter image description here

In this situation, [M, F] = mode(d) gives a result that I didn't want.

Is there any function that counts the most frequent values which takes a sort of tolerance into account?

Clustering can be considered. However, In the figure above, clustering may assign d(27:29) into the left side cluster.

Current approach is normilizing and thresholding:

d_norm = d / max(d);
v = d_norm(d_norm < 0.01); % 1 percent threshold

However, I think it is a sort of hard-coded and not a good approach.


Solution

  • Histcounts is your friend!

    Your "threshold" can be easily translated to histogram bins if you know your range. In your case, the range is 0-1, if you choose a threshold of 0.01 then 100 is your bin count.

    counts=histcounts(d,100)