help mata mm_locate()


mm_locate() -- Search an ordered vector


void mm_locate(y, x, j)

void mm_hunt(y, x, j)


y: numeric vector containing data; y must be ordered, either ascending or descending

x: numeric scalar containing value to be located in y

j: real scalar containing initial guess for mm_hunt(); will be replaced by a real scalar such that x lies between y[j] and y[j+1]


Given an ordered data vector y and some value x, mm_locate(), replaces j such that x lies between y[j] and y[j+1]. j==0 or j==length(y) is returned, if x is out of range. mm_locate() applies the bisection method to locate x in y.

mm_hunt() also locates x in y. However, mm_hunt() uses the input value of j as the starting point for the search process and applies a modified algorithm. mm_hunt() is useful if consecutive searches be performed for similar values.

mm_locate() is a translation of the locate algorithm in Press et al. (1992:117). mm_hunt() is a translation of the hunt algorithm in Press et al. (1992:118-119).




mm_locate(y, x, j), mm_hunt(y, x, j): input: y: n x 1 or 1 x n x: 1 x 1 j: 1 x 1 output: j: 1 x 1



Source code



Press, William H., Saul A. Teukolsky, William T. Vetterling, Brian P. Flannery (1992). Numerical Recipes in C. The Art of Scientific Computing. Second Edition. Cambridge University Press.


Ben Jann, ETH Zurich,

Also see