Faster way of writing this code: (finding index)

4 次查看(过去 30 天)
The code I want to optimize is:
index_min = sum( input > quantiles );
where quantiles is a vector of doubles in increasing order. I am using the sum function to find the index of quantiles that corresponds to the value of input. I think the "find" function is actually slower.
  2 个评论
Roger Stafford
Roger Stafford 2012-12-22
Since the quantiles are in ascending order, it would likely be faster to use the 'histc' function with quantiles as the 'edges' vector and input as the first argument. The second returned argument (bin) would index the appropriate bin and thereby give you the index you seek.
Image Analyst
Image Analyst 2012-12-22
编辑:Image Analyst 2012-12-22
Just how much speed do you require? Is this for a real time app, like for video processing? How much slower? Are we talking nanoseconds here?

请先登录,再进行评论。

回答(2 个)

Matt Fig
Matt Fig 2012-12-22
That code does not return an index, but it does return the number of values in vector quantiles that are smaller than input.
  2 个评论
Walter Roberson
Walter Roberson 2012-12-22
Because the values are in order, this is equivalent to returning the index of the last such value.
Matt Fig
Matt Fig 2012-12-22
Yes, it will return the last value. How large is quantiles?

请先登录,再进行评论。


Matt J
Matt J 2012-12-23

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by