finding approximate indices in a monotonically increasing array

4 次查看(过去 30 天)
I need to find the indices of an entry in a monotonically increasing array, where the value of this indices is "ref"(as shown below)
I create my increasing array "t" as follow
dt=1/10e9;
N=13600000;
t=((0:N-1)*dt);
ref=1.39459e-05;
I've already tried using the find function but it leads to incorrect results or multiple indices or no answers at all.
can anyone suggest any way I can get this working

采纳的回答

Shashank Prasanna
Shashank Prasanna 2013-1-30
You can use Nearest Neighbor search. If you have stats toolbox:
f = knnsearch(t',ref)
f =
139460
>> t(f)
ans =
1.39459e-05
IF you don't have the stats toolbox:
>> tri = delaunayn(t');
f = dsearchn(t',tri,ref)
f =
139460
Because you have so many points you have to be patient since it takes time.
  2 个评论
Sean de Wolski
Sean de Wolski 2013-1-31
There is so much extra working put into calculating the Delaunay triangulation for all of t when all you need is a simple histogram calculation of one element! I would not recommend this approach.
Foosball?
Shashank Prasanna
Shashank Prasanna 2013-1-31
I agree, i just like using nearest neighbor search, and it always works. lets go.

请先登录,再进行评论。

更多回答(3 个)

Sean de Wolski
Sean de Wolski 2013-1-30
[~,idx] = histc(ref,t);
Use histc() to find the bin containing ref.
  1 个评论
Jan
Jan 2013-1-31
HISTC is a fast and efficient C-Mex function, which does not create the temporary vector abs(t - ref) and performs a binary search, which is the optimal strategy in theorie. +1

请先登录,再进行评论。


Jan
Jan 2013-1-31
dt = 10e-9;
N = 13600000;
t = (0:N-1)*dt;
ref = 1.39459e-05;
[value, index] = min(abs(t - ref));
But without doubt, the binary search of histc is smarter than comparing all values in this brute force approach.

Arsalan
Arsalan 2013-1-31
编辑:Arsalan 2013-1-31
Hi Guys, thanks for your suggestions, I never knew about the use of some of this commands before. but I found much more efficient way
dt=1/10e9;
N=13600000;
t=((0:N-1)*dt);
ref=1.39459e-05;
index=round(ref/dt);
my_value=t(index); % should be approximately equal to ref
it works well for values greater then zero, but should work for values less then zero with some manipulation

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by