Most populated range of floating point numbers in array

2 ビュー (過去 30 日間)
lvn
lvn 2014 年 9 月 8 日
コメント済み: lvn 2014 年 9 月 9 日
histc can be used on a floating point array to find the bin with the largest number of elements. However these bins are fixed, and for a fixed width of bin, might not be optimal.
Example
a=[0 0.01 0.4 0.45 0.55 0.56 0.60]
histc(a,[0 0.5 1])
ans =
4 3 0
So the most frequent bin is [0, 0.5]. However, I am interested in a function that finds the range of at most 0.5 wide, with the most elements, so in this case [0.4, 0.6] which contains 5 elements. Does anybody know an elegant way of doing this?

採用された回答

Roger Stafford
Roger Stafford 2014 年 9 月 8 日
Perhaps you won't consider this for-loop solution elegant, but it should be computationally efficient. I will assume that your array 'a' is already in ascending order, as in your example. If not, you should sort it first before using the following code:
d = 0.5; % <-- or whatever you choose
n = length(a);
i1 = 1;
for i2 = 1:n
if a(i2)-a(i1) <= d
m = i2;
else
i1 = i1+1;
end
end
The interval [a(m-n+i1),a(m)] is of width less than or equal to d and contains the maximum number of points among such intervals. If there are other such intervals with the same number of points, this is the first one encountered.
  1 件のコメント
lvn
lvn 2014 年 9 月 9 日
Fantastic! My solution was a very slow double for loop over all begin and end combinations, and for my purposes just too slow. Yours works like a charm. Thank you very much!

サインインしてコメントする。

その他の回答 (1 件)

Honglei Chen
Honglei Chen 2014 年 9 月 8 日
You can just do
histc(a,[0 0.4 0.6 1])
if you know for sure you want the interval [0.4 0.6]. Otherwise, you can use hist
y = hist(a,[0 0.5 1])
which specifies the center
  1 件のコメント
lvn
lvn 2014 年 9 月 8 日
Thanks for the answer, the problem is that I do not know in advance the boundaries of the interval (from one run to another, the vector attains different values). Only the maximum width of the interval is known.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by