MATLAB Answers

Evan
0

Faster way of writing this code: (finding index)

Asked by Evan
on 22 Dec 2012
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 Comments

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.
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?

Sign in to comment.

2 Answers

Answer by Matt Fig
on 22 Dec 2012

That code does not return an index, but it does return the number of values in vector quantiles that are smaller than input.

  2 Comments

Because the values are in order, this is equivalent to returning the index of the last such value.
Yes, it will return the last value. How large is quantiles?

Sign in to comment.


Answer by Matt J
on 23 Dec 2012

  0 Comments

Sign in to comment.